I am having some problems understanding the formatting of binary files that I am writing using Fortran. I use the following subroutine to write binary files to disk:
SUBROUTINE write_field(d,m,outfile)
IMPLICIT NONE
REAL, INTENT(IN) :: d(m,m,m)
INTEGER, INTENT(IN) :: m
CHARACTER(len=256), INTENT(IN) :: outfile
OPEN(7,file=outfile,form='unformatted',access='stream')
WRITE(7) d
CLOSE(7)
END SUBROUTINE write_field
My understanding of the access=stream
option was that this would suppress the standard header and footer that comes with a Fortran binary (see Fortran unformatted file format).
If I write a file with m=512
then my expectation is that the file should be 4 x 512^3 bytes = 536870912 bytes ~ 513 Mb
however they are in fact 8 bytes longer than this, coming in at 536870920 bytes
. My guess is that these extra bytes are the 4 byte header and footers, which I had wanted to suppress by using access='stream'
.
The situation becomes confusing to me if I write a file with m=1024
then my expectation is that the file should be 4 x 1024^3 bytes = 4294967296 ~ 4.1 Gb
however they are in fact 24(!) bytes longer than this, coming in at 4294967320 bytes
. I do not understand why there are 24 extra bytes here, which would seem to correspond to 6(!) headers or footers.
My questions are:
(a) Is it possible to get Fortran to write a binary with no headers or footers?
(b) If the answer to (a) is 'no' then can I ensure that the larger binary has the same header and footer structure as the smaller binary?
(c) If the answers to (a) and (b) are both 'no' then how do I understand where these extra headers and footers are in the file.
I am using ifort
(version 14.0.2) and I am writing the binary files on a small Linux cluster.
UPDATE: When running the same code with OSx
and compiled with gfortran
7.3.0 the binary files come out with the expected sizes, as in they are always 4 x m^3 bytes
, even when m=1024
. So this problem seems to be related to the older compiler.
UPDATE: In fact, the problem is only present when using ifort
14.0.2 I have updated the text to reflect this.