I have a binary file generated by fortran code. This file contains an array of doubles. I need to open it in my C program and then work with it as with an usual array.
How do I do it?
How double type is represented in binary file?
I have a binary file generated by fortran code. This file contains an array of doubles. I need to open it in my C program and then work with it as with an usual array.
How do I do it?
How double type is represented in binary file?
Almost all Fortran compilers, for unformatted (i.e., binary) sequential files, write the length of the record at the beginning and end of each record. Typically the length is written in four bytes, though some compilers use eight bytes. A record is everything that is written with a single Fortran write statement. If this array was written with a single Fortran write statement with this type of file, then you have four or eight bytes to skip over, then the array, then four or eight extra bytes. You can probably figure this out by viewing the file with a hex editor. If the array was written in pieces (e.g., by columns or rows) with multiple write statements, each record will have this structure.
If the file is written by Fortran as unformatted, the Fortran double type is written in the binary representation of that computer. You should be able to read it in C on the same computer type. If you are changing architectures and not just languages the binary representation might be different.
Fortran has other file access methods which don't include the record length information. Direct access, for example. The new method of stream access is designed for inter-language compatibility.
If the array is multi-dimensional you may want to transpose it because Fortran has column-major arrays while C uses row-major.
Related questions: Fortran unformatted file format and reading fortran unformatted file with python
You can't, in principle.
The format of Fortran binary files is unspecified. That means that, although you can save program state in a Fortran binary file, and reopen it using the same Fortran implementation, you can't reliably do so even with a different Fortran implementation.
Now, there are a limited number of ways that one can sanely save state, so it's both possible and probable that you'd be able to work out the structure of a given Fortran binary file, in enough detail that you can open it in a C program and read the contents. However that knowledge is completely specific to the Fortran compiler in question.
So the best thing to do is to create a Fortran array containing, say, [0, 1, 2, 3, 4, 256, 65536, 65537, 1e10, 1e20, 1e30, 1e40]
, write it to a binary file, and look at it with a binary editor to see what's there.
After that, you're on your own I'm afraid....
In my own opinion, the better way to read fortran binaries is actually to read them from from fortran and pass array to C. It can save you a lot of pain.
The hardest part is to wrap your fortran subroutine. Name mangling is not straight forward between C and fortran. It depends on the compiler (i.e. platform) and Fortran version you use. You can look here and here for simple examples.
First choice, is to do it by "hand" with the appropriate compilation options.
Tips : to know the mangling you can use nm command in linux or a tool like DLL export viewer.
A better choice is to use Fortran 2003 which introduce the module iso_c_binding which helps to handle this kind of operation.