How do I read fortran binary file in C?

后端 未结 3 441
遥遥无期
遥遥无期 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 04:50

    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.

    1. Write a subroutine that actually can read this binary
    2. Wrap your subroutine to call it from C.

    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.

提交回复
热议问题