How do I read fortran binary file in C?

后端 未结 3 435
遥遥无期
遥遥无期 2021-01-20 04:33

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.

3条回答
  •  隐瞒了意图╮
    2021-01-20 05:02

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

提交回复
热议问题