Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

EOF with Stream I/O

efengle
Novice
973 Views
I have a very simple example as a way to ask this question.
[fortran]program test
implicit none

integer(KIND=8) :: val
open(unit=10,file="fort.10",form="unformatted",access="stream",status="old")
read(10,POS=1)val
write(6,*)'VAL = ',val
close(10)
stop
end program test[/fortran]
The file being read, fort.10, was created by another simple FORTRAN program. That program wrote a 4-byte integer with a value of 65 as stream output, so there is no FORTRAN header/trailer record information in the file.
When the above program is executed, I get an EOF error. I think I can understand why. Does the READ statement instrinsically try to fill all N-bytes of the variable (8 in this case) from the input file? Recall the input file only contains 4-bytes
Is there a way to specify how many bytes I want to READ statement to read?
This is just a test case, but ultimate goal I am working on is reading variable length records.
Here is the program to create the file.
[fortran]program test_write
implicit none

integer(KIND=4) :: val
open(unit=10,form="unformatted",access="stream",status="new")
val=65
write(10)val
close(10)
stop
end program test_write
[/fortran]
Thanks,
Eric
0 Kudos
4 Replies
mecej4
Honored Contributor III
973 Views
For unformatted and stream I/O, the number of 'storage units' read/written is determined by the size of the entities in the I/O list. The types of the variables in the I/O list are not significant, only their cumulative size. Thus, your example is hardly different from one in which you wrote ten bytes into a file and tried to read twenty using CHARACTER(len=10) and CHARACTER(len=20) variables.

In your example, the READ requires eight or more bytes to be present in the remainder of the file. Trying to read more bytes than present in the file will cause EOF. You can catch and process the EOF, but note that some implementations may not let you rewind or backspace stream files.
0 Kudos
efengle
Novice
973 Views
Funny how it makes more sense when someone else explains it to you. Anyways, at work we use IBM XL Fortran. They have a specific IBM extension in the READ statement. You can specify a NUM= keyword specifies the number of bytes to transfer between the I/O list and the file.
That would be a great feature to add the Intel Fortran.
0 Kudos
jimdempseyatthecove
Honored Contributor III
973 Views
What is this supposed to do with the rest of the I/O list?
Zero fill? Don't touch?

What do you want it to do when reading unformatted REAL(4) into REAL(8)?

For your purpose you could read into a character array, then pick 1, 2, 4, 8, 16, ...bytes as you wish
Use TRANSFER or UNION to "cast" types from character to type of your choice.

Jim Dempsey
0 Kudos
mecej4
Honored Contributor III
973 Views
If you must do byte-juggling in a Fortran application:

Many Fortran compilers let you call routines in the system C runtime, such as fread() and fwrite(), and the C-interoperability of F2003 makes that easier.

Whether to use a vendor extension to Fortran or to use standard C is a portability issue whose costs/benefits need to be assessed.
0 Kudos
Reply