Stream I/O usage

I have been investigating the use of STREAM IO,

  • as a replacement for ACCESS=‘DIRECT’ to have variable length “data records” and also
  • to remove portability issues for ACCESS=‘SEQUENTIAL’, FORM=‘UNFORMATTED’ by removing the record header/footer.

ACCESS=‘STREAM’ provides for both sequential processing or direct processing by including POS=address.
The problem I have for an ACCESS=‘STREAM’ file is due to this alternative use and include:

  1. Should POSITION= be allowed when opening the file ? Modern Foprtran Explained describes position= as “The access method must be sequential”, but sequential is not bold.
  2. Should the file be truncated on WRITE if being accessed sequentially ? I think this is a consequence of ACCESS=‘SEQUENTIAL’ but can not find this clearly defined.

Should these problems be resolved by the way the file is subsequentially accessed in READ or WRITE usage (ie if POS= is used) or should there be further options in OPEN to resolve these issues ?

1 Like

“STREAM” implies “SEQUENTIAL”, as without explicit positioning the fie pointer reads/writes where it left off. The main difference is that “STREAM” assumes no structure whatsoever, you have to define it yourself. That said:

  • POSITION= is allowed, because it is a sequential file.
  • The file is not automatically truncated if you write to it. That is, it is possible to update the file, like you can with direct-access files.
  • If you want a fresh start, specify STATUS = ‘REPLACE’ to force the file to be recreated.

(fortran90 - overwrite a file using fortran - Stack Overflow may be useful)

1 Like

@Arjen,

Thanks for your reply and the reference to StackOverflow.

I think it could also be said that “STREAM” implies “DIRECT”, as POS= provides explicit positioning.

POSITION=‘append’ might also be valid for OPEN, because it should be possible to continue to update the file as a sequential file.

The problem is in what circumstances should, when re-opening an existing access=“stream” file, that it would be truncated after a WRITE.
Could it depend on any subsequent use of POS= ?
Although it is not necessary to use POS= to overwrite the file, such as REWIND (lu) ; WRITE (lu) number.
I have not identified where this is explained in the latest standard.

You have probably searched the standard better than I have, but my experience is that the file is NOT truncated on writes. You really need a STATUS option to achieve that. In that respect it is indeed like a direct-access file and I suppose that behaviour is inline with binary files in C (though there you would probably be very explicit when opening the file).

Some of the statements above are only try for one type of stream file or the other – there are two; with significant differences – formatted and unformatted. The Fortran Wiki article might be interesting …

https://fortranwiki.org/fortran/show/Stream+Input+Output

@urbanjost, thanks for the Wiki reference.

Experimenting with STREAM I/O quickly shows the powerful range of capabilities, from first scanning a file by byte to identify the data structures, to easily developing a set of routines that can recognise the data structures in files from other sources, such as the .DBF example.

This gives a way to decode these file types which was so difficult to interpret before F03.