Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
530 Views

Difference between form='unformatted' and form='binary'?

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!

0 Kudos
7 Replies
Highlighted
Black Belt
530 Views

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.

0 Kudos
Highlighted
530 Views

FORM='BINARY' should not be used anymore - instead use the standard ACCESS='STREAM' (with FORM='UNFORMATTED').

[Edited to correct keyword from RECORDTYPE to ACCESS]

0 Kudos
Highlighted
Beginner
530 Views

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!

0 Kudos
Highlighted
Beginner
530 Views

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...

0 Kudos
Highlighted
Black Belt
530 Views

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.

0 Kudos
Highlighted
530 Views

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.

0 Kudos
Highlighted
Beginner
530 Views

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.

0 Kudos