- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page