Leif Harcke
2005-04-24 02:03:54 UTC
Suppose you have a number of binary, fixed length records in a file.
The number of such records is unknown at run time, but the record size
is known or provided. How do you prevent yourself from running off
the end of the file?
Is there a way to detect "end of file" for direct access, unformatted
files? The Fortran standard requires IOSTAT < 0 be returned only for
sequential files that read an EOF record. For direct access files, no
such requirement to return IOSTAT < 0 exists.
After investigating the behavior of a number of f77 and f90 compilers
on different UNICES, it appears some run-time systems generate an
ERROR, while some set IOSTAT=-1 upon reading past the last record in a
direct access, unformatted file.
Looks like the best portable way to avoid running off the end is to
get the file size via a call to the IOSTAT intrinsic and then divide
by the known record size to get the maximum possible record number.
Then, make sure you never exceed this number on a READ statement. Of
course, IOSTAT isn't standard, but most UNIX-based compilers seem to
provide it. Any other suggestions?
-Leif
The number of such records is unknown at run time, but the record size
is known or provided. How do you prevent yourself from running off
the end of the file?
Is there a way to detect "end of file" for direct access, unformatted
files? The Fortran standard requires IOSTAT < 0 be returned only for
sequential files that read an EOF record. For direct access files, no
such requirement to return IOSTAT < 0 exists.
After investigating the behavior of a number of f77 and f90 compilers
on different UNICES, it appears some run-time systems generate an
ERROR, while some set IOSTAT=-1 upon reading past the last record in a
direct access, unformatted file.
Looks like the best portable way to avoid running off the end is to
get the file size via a call to the IOSTAT intrinsic and then divide
by the known record size to get the maximum possible record number.
Then, make sure you never exceed this number on a READ statement. Of
course, IOSTAT isn't standard, but most UNIX-based compilers seem to
provide it. Any other suggestions?
-Leif