I am trying to fully understand how to deal with binary files in Fortran. Specifically, I am reading this https://atmos.washington.edu/~salathe/osx_unix/endian.html which covers how to write binary files when the records to be written are fixed-length. This case is intended to allow a raw format that can be easily read/writen from/to other programming languages given that the endianess of the systems are the same (which is in the case in 2021, as as far as I know most practical systems are Little Endian now).
In the article, it is said to open the file using direct
and unformatted
.
open (unit=1, file='out.r4', form='unformatted', access='direct', recl=m*n*sizeofreal)
If I understood correctly how it works, the recl
parameter is used to control the size of the records intended to be written.
I have several doubts about how recl
shall be used in Fortran:
- Is it the size of the record in bytes?
- Does it depend on the compiler?
- Does it depend on the Fortran Standard we are using?
- If so, what are the differences among standards?
- Does the meaning of
recl
depend if we are writing derived types or "regular" types?
My experimentation suggest that using the bytes
size of the derived type when writing derived type records is adequate; but I do not know if that is dependant on the specific compiler I am using or not.
By default I am trying to do things using flang
and Fortran 95 (just to learn a standard which is fully compatible with f2py
).