0

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

M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 1
    Take a look at [Reading writing fortran direct access unformatted files with different compilers](https://stackoverflow.com/questions/32686720/reading-writing-fortran-direct-access-unformatted-files-with-different-compilers). I am very tempted to suggest this as a duplicate - my only concern is that off the top of my head I am not sure when inquire by output list came into the language, either Fortran 90 or Fortran 2003. – Ian Bush Jun 01 '21 at 08:07
  • So if I understood that post correctly, no standard dictates what `recl` shall be (bytes or block size) and it is compiler/system dependent. But, you can inquire `iolength` to get the actual IO recl size in your system. You just do not know which standard that belongs. From the linked post: "The other alternative is to not worry about the option and use the inquire intrinsic to query the iolength for your array. " – M.E. Jun 01 '21 at 08:13
  • 1
    Correct. Pad ... pad ... – Ian Bush Jun 01 '21 at 08:13
  • Would the `iolength` inquire return the `recl` block size in bytes? – M.E. Jun 01 '21 at 08:14
  • 1
    And I'm going to say it again, I would strongly encourage you to move to at least Fortran 2003 - there are at least two free, good quality compilers out there, hamstringing yourself by sticking to what is now a very old version does seem strange to me. – Ian Bush Jun 01 '21 at 08:15
  • 1
    No. In the appropriate units for the compiler to use in a RECL= for an open of a direct access file – Ian Bush Jun 01 '21 at 08:15
  • I am not fully sure that you can freely use Fortran 2003 with `f2py`, which is part of my agenda. Even if I move to Fortran 2003, I like to understand the standards (same for C, I try to clearly understand different standard changes). – M.E. Jun 01 '21 at 08:16
  • 2
    I think that the poster might wish to consider stream access instead of direct. I suspect that the webpage that is cited may have been written before stream existed. Steam will achieve the compiler/language portability goals, without needing the record length information. If you want fixed length records, just write fixed length records. – M. S. B. Jun 01 '21 at 08:31
  • 1
    Absolutely, firect access wss often used as poor-man's stream access before F2003. – Vladimir F Героям слава Jun 01 '21 at 08:33
  • I do not know if that is applicable in Fortran but in C++ and for my particular environment using streams was slower. In my case I have to deal with fixed-width records. For reading I check the size of the file and I read the whole array at once, which is very useful for my particular use case. – M.E. Jun 01 '21 at 08:35

0 Answers0