Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read subroutine 1

Status
Not open for further replies.

Lloydone

Programmer
Jan 17, 2007
41
NZ
hey people,
doing some research with HDF files, I want to list the file names within a file called say 'names.txt'
when using the below code, when i alter the number of file names the code works occassionally and other times it doesnt. can you see why ?

subroutine readme (aString)!input would be names.txt
use L2Parameters !Array 'holder' defined here
implicit none
character(len=60) :: aString
integer::n,ios

print*,'Opened readme'
open(9,file=aString,status='old')

do n=1,size(holder)
read(9,*,iostat=ios,err=100)
enddo
if (ios /= 0) stop 'I/O problem reading too file'
100 print*,'Error occured reading from file'


close(9)
end subroutine readme
 
At a guess it is your filename. Print out aString. Does it have any spaces in the filename? Is that what you're expecting? If you are forming the filename from a write statement or concatenating strings, make sure you ltrim the string containing the number first.
 
thanks for your reply xwb, will have a look.
Regards
lloyd
 
xwb,
I took your reply into consideration and found no problems with the file name. I think the problem instead lay in the fact that i needed to change:

subroutine readme (aString)!input would be names.txt
use L2Parameters !Array 'holder' defined here
implicit none
character(len=60) :: aString
integer::n,ios

print*,'Opened readme'
open(9,file=aString,status='old')

do n=1,size(holder)
read(9,*,iostat=ios,err=100)! <-------- problem
enddo
if (ios /= 0) stop 'I/O problem reading too file'
100 print*,'Error occured reading from file'


close(9)
end subroutine readme


i needed to include an end statement that was as follows:

read(9,*,iostat=ios,err=100, end=101)
...
101 continue



after I did this it went well

regards

Lloyd
 
Lloyd,

your code is somewhat confusing.

'end = label' or 'err = label' clauses are repalaced by 'iostat = variabel' in modern Fortran ( I guess in Fortran 90).

iostat passes negative values to its associated variable any time the read fails for whatever reson, and 0 if the read went okay.

You specified iostat to pass the error value to ios and in the later 'if' to abort execution if ios is not 0 - that is on any failure to read. But apparently this is not what you wanted because your end = and err = clauses specify some statement labels and thus to continue operation of the program.

End = and Err = seem to supercede but I would not rely on this. In fact you specified two different actions in case of end of file or error.

In case you want to read your file and continue once it is read completely your code should be something like

character (len = ...) value (nmax)
integer n,ios

open (unit=9,file=myfile,status='old')
rewind 9
n=0
do
n=n+1
if(n.gt.nmax) exit
read (9,formatlabel,iostat=ios) value(n)
if(ios.ne.0) exit
enddo
(next statement)

with nmax being an integer constant or variable.

This reads all the lines thare are in your file and passes control to the statemnt after the enddo once all the lines are read.

Norbert
 
Thanks for your response Norbert,

does iostat clause just check for I/O errors, while end clause is used when no more data may be read?

also, i have not been brave enough to use rewind, could you maybe explain it

Regards

Lloyd
P.S. I am using f95
 
'rewind (unit)' is just a habit, deriving from times when we had magnetic tapes to save our data (I am a living fossil from that time). It ensured that the tape was rewound and positioned at the beginning of the data.

According to the documentation of Fortran 95 the file after it is opened is positioned to read the first record . But still....

You can use 'rewind' any time when you want to return to the beginning of the file and restart reading from the beginning (to find a special record for instance).

'iostat = variable' checks for all the errors that can happen in the i/o statement where it is used (read, open, close etc.). However it looks like your compiler produces code that finishes processing the read statement completely before it takes the next action. And then 'err = label' or 'end = label' seem to redefine the action because you placed them behind your 'iostat = ' clause. For sure, this is not standard Fortran and I would not rely that all compilers will come to the same output.

Norbert
 
Thanks for the explaination Norbert,

I am going to refine my code now, and will use your recommendations

regards

Lloyd
 
Norbert you used used formatlabel as a clause in your read operation,
can you please explain?
 
No,no.

read (9,formatlabel,iostat=ios) value(n)

Sorry, I did not explain. 'formatlabel' is to be replaced by a number indicating a label to a format like

read(9,10,iostat=ios) valkue(n)
10 format(f10.2)

Norbert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top