Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

help reading text files EOF error

cfox28
Beginner
984 Views
Hi 

I am trying to create and read a text file in fortran 90
the file is created ok and just contains some numbers for now
but when I try to read it back in I get a run time error
 
"attempt to read past end of file"
how do I read the file back in 
the code I'm using is below

Cheers

CHRIS

open (55,file='output.txt',form='FORMATTED')
WRITE(55,*) anat
close(55)

open (56,file='output.txt',form='FORMATTED')
READ (56) anatomyNumbers
close(56)

0 Kudos
7 Replies
TimP
Honored Contributor III
984 Views
According to what you posted, your READ(56) expects unformatted data file. If you write with fmt=*, you could try to read back into an identically declared datum, using a similar format.
0 Kudos
cfox28
Beginner
984 Views

Hi

Thanks that helped I can now read the data using

open (2,file='output.txt')
READ (2,*) anatomyNumbers(1:13)
close(2)

However is there a way to do it in which the 1:13 i.e. the

number of values in the file doesn't have to be specified.

eventually my files will be large and I will not know how many

values are in them.

Also when I define

INTEGER, ALLOCATABLE :: anatomyNumbers(:)

ALLOCATE(anatomyNumbers(13))

the third value has a 12 in it before I do anything to it

Cheers

CHRIS

0 Kudos
Steven_L_Intel1
Employee
984 Views
You don't need the (1:13) - it will read as many elements are in the array.

When you ALLOCATE an array, the initial contents are undefined. They could be anything. Zero the array after allocation of that's what you want.
0 Kudos
Intel_C_Intel
Employee
984 Views

Hello,

To catch most IO-errors in a controlled way, you can use ERR and IOSTAT in the READ statement, for example like this:

READ (*,*, ERR=999, IOSTAT=IERR) A,B,C

IF (IERR .NE. 0) GOTO 999

Then you can decide what to do yourself if there is something fishy with the file you are reading.

I hope this works,

Lars Petter Endresen

0 Kudos
Steven_L_Intel1
Employee
984 Views
If you're using IOSTAT and a test like that, you don't need ERR= also. Do keep in mind that an ERR= branch will NOT be taken on an end-of-file condition - you need END= for that.
0 Kudos
cfox28
Beginner
984 Views

Thanks for the help with this problem I got it to work using

open (4,file='output.txt',FORM='FORMATTED')
write(4,*) nvoxels
close(4)

open (3,file='output.txt',FORM='FORMATTED')
read(3,*,IOSTAT=isEND) anat
if(isEND.NE.0) GOTO 999
999 close(3)

Is there anything differnt that must be done to create and read a binary file
instead of text
and could anyone recommend a good fortran 90 programming book please
Thanks to all
CHRIS
0 Kudos
TimP
Honored Contributor III
984 Views
ifort supports the common extension form='binary', which you can read about in the documentation. The traditional Fortran form='unformatted' is a style of binary, but the files aren't portable in general between systems. Both access='direct' and 'sequential' are commonly used with 'unformatted'
The following URLs include many recommendations about textbooks and on-line learning:
http://www.fortran.com/
http://www.polyhedron.com/
The Lawrence book discusses Windows-specific details of Intel compilers.
Metcalf/Reid/Cohen is an unusual combination of suitability for learning, with authoritative accuracy, and errata updates available on line.
0 Kudos
Reply