- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Does anyone know what is the exact difference between the form='unformatted' and form='binary' in open() when writing/reading raw binary files? Seems that form='binary' is not available in fortran compilers such as gfortran. I read the documentation for FORM in https://software.intel.com/en-us/node/525755#E6F9FE02-2CA6-468D-B8D5-E2AA56FF2CC1, but still do not quite understand the difference. For stream access read/write, is there any difference between using form='unformatted' and form='binary'?
Thanks!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unformatted files contain record-length information, encoded in a vendor-specific way, interspersed with the data, in the file.
Files with FORM='BINARY' are nonstandard, and contain no record-length information. Such files may have been written by a C program after having been opened with fopen(<file_name>,"wb") (or otherwise), and being able to use FORM='BINARY' allowed stream access in the decades before stream access was added to the Fortran standard.
One has to look at the history of I/O devices from the 1950-s to today in order to see why the file OPEN statement has such a large variety of optional clauses. The old tape and DASD files were not at all in harmony with the "stream of bytes" view that Unix adopted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
FORM='BINARY' should not be used anymore - instead use the standard ACCESS='STREAM' (with FORM='UNFORMATTED').
[Edited to correct keyword from RECORDTYPE to ACCESS]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve Lionel (Intel) wrote:
FORM='BINARY' should not be used anymore - instead use the standard RECORDTYPE='STREAM' (with FORM='UNFORMATTED').
Thanks, Steve. May I know why the FORM='binary' is not recommend to be used anymore in modern Fortran? If I want to read/write stream file, can I use ACCESS='STREAM' + FORM='UNFORMATTED' without RECORDTYPE?
Also, for direct access, if a file was written with ACCESS='DIRECT' + FORM='BINARY', could it be read seamlessly with (recommended) ACCESS='DIRECT' + FORM='UNFORMATTED'?
Thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
mecej4 wrote:
Unformatted files contain record-length information, encoded in a vendor-specific way, interspersed with the data, in the file.
Files with FORM='BINARY' are nonstandard, and contain no record-length information. Such files may have been written by a C program after having been opened with fopen(<file_name>,"wb") (or otherwise), and being able to use FORM='BINARY' allowed stream access in the decades before stream access was added to the Fortran standard.
One has to look at the history of I/O devices from the 1950-s to today in order to see why the file OPEN statement has such a large variety of optional clauses. The old tape and DASD files were not at all in harmony with the "stream of bytes" view that Unix adopted.
Thanks for the reply. Seems complicated and I need to do a research on this...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
form='binary' was a vendor-dependent extension for which 'stream' files were adopted as a standard replacement. You might consider replacing files written that way by reading (with the same vendor's compiler) with the 'binary' options and writing out with current standard options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As Tim says, FORM='BINARY' was an extension - Microsoft's, I think - that got picked up by others. There is no reason to use it now that the standard has ACCESS='STREAM' (this is what I meant instead of RECORDTYPE='STREAM'). Both are just "a stream of bytes". There is no need to read with one and write with the other.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Theses two OPEN statements are equivalent when writing a file:
- Non-standard: open(unit=20,file=fname,form='binary')
- Standard: open(unit=20,file=fname,form='unformatted',access='stream',status='replace')
Note that without status='replace', it defaults to 'UNKNOWN', which is "processor dependant" (according to the Fortran 90 Standard). I have found with ifort and gfortran on Linux that if a file already exists, it is not deleted first, and the bytes just replace over the old file. And if a smaller number of bytes are written, the remaining parts of the file remain (i.e. the file is a mess). Therefore I recommend adding "status='replace'" to make a compatible switch from non-standard "form='binary'" to unformatted stream access files.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page