Read reals from binary file created by Fortran

Hello!
I’ve created a binary file using Intel Fortran (Parallel Studio)+Visual Studio (x64). The file stores 3d array of real numbers and has very simple structure: first three numbers stored in the file are integers (kind=4) - they are dimensions of the array. Other numbers are reals (also kind=4). To write them into the array in Fortran I used functions

open(1, FILE=path, iostat=IOS, action=“WRITE”, form=“BINARY”, recl=4)

write(1) imax; write(1) jmax; write(1) kmax;

write(1) arr(i,j,k)

To read this file in Mathematica I used
{N1, N2, N3} = BinaryReadList[filename, “Integer32”, 3]
for integers and they were read succesfully.
The problem is that real numbers were read incorrecly when I’ve used
BinaryReadList[filename, “Real32”, 1]
for each of them. Changing the option “ByteOrdering” doesn’t help.

Pavel,

I would think that you should use Real64.

Regards

Neil

No, that doesn’t work. And I’ve mentioned that I’ve used real (kind=4) in Fortran that takes 4 bytes of memory.

I would open the file in a hex editor and the format will be clear. You can select the bytes of a number that was written and have the editor give you the value in various formats. From this you can determine what you have and how to read it.

To be of anymore help, you would have to post a sample file and what numbers you wrote to it.

Regards,

Neil

Also, I do not agree that you are specifying a record length of 4. According to the Fortran specification opening using form=BINARY overrides the recl specifier. I believe that Binary writes W*64 on 64 bit machines (but I am not certain without examining a file).

Thank you,
I’ve opened the file in Matlab, that may be even better for my current needs. But I will examine the file structure and Fortran options.

Hello Pavel,

At issue is you need to account for the presence of record markers in the Fortran binary file. For each Fortran Write statement, a record is streamed out, consisting of start and end record markers, and the record payload in between. The markers are 4-byte integers with value equal to #bytes of the payload. It may be that on your architecture, the markers are 8-byte integers.