I have a unformatted binary data file generated by gfortran. However, when the binary data is imported by Mathematica, a correct result is not obtained.

This is my fortran code:

  Module Output
    interface export
      module procedure export_IMA1
    end interface

    subroutine export_IMA1(filename, ma)
        character(*), INTENT(IN)::filename
        integer, intent(in)::ma(:)
        open(unit=55,file=filename//".dat", status="replace",&
        write(55) ma
    end subroutine export_IMA1
   end module Output

    Program Test
    use output
    implicit none
    call export("c",c)
    end program

The fortran is simplified, such a code just stores the integer array c=[1,2,3,4] in a stream style. The Mathematica code is

       filename3 = "Code/c.dat", 
  str = OpenRead[filename3, BinaryFormat -> True];
  l = BinaryReadList[str, "Integer32", 4, ByteOrdering -> $ByteOrdering];

and the result l=1 not l=[1,2,3,4]. How do you correctly import the binary data (stream) generated by the Fortran code into Mathematica?

I show c.dat (c=[1,2,3,4]) in vim after hexdump as following:

enter image description here

and the text form is https://pastebin.com/x5m0dXsK


closed as off-topic by MarcoB, m_goldberg, xzczd, Carl Lange, Alex Trounev Mar 15 at 14:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, m_goldberg, xzczd, Carl Lange, Alex Trounev
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Can you explain the format of the data for those of us who are not very familiar with Fortran? A quick google search seems to tell me that integer*1 means 8-bit integers in Fortran, but you're trying to read 32-bit ones in Mathematica. $\endgroup$ – Szabolcs Mar 4 at 18:03
  • 1
    $\begingroup$ Could you give an example of the data - the first few lines, perhaps? $\endgroup$ – Carl Lange Mar 4 at 18:54
  • 2
    $\begingroup$ Please post the data as text rather than as an image. $\endgroup$ – Carl Lange Mar 4 at 19:13
  • 2
    $\begingroup$ I used the provided fortran code to recreate the file c.dat. This imports in Mathematica with no issue for me with BinaryReadList[str, "Integer32", ByteOrdering -> -1] $\endgroup$ – chuy Mar 4 at 20:42
  • 1
    $\begingroup$ Hi gays! Thanks for your help! I figure out what my problem is. I got l=1 due to evaluating it in a new cell. If adding a sentence "print[l];" in the Module. I will get the correct result. $\endgroup$ – finalboss Mar 4 at 21:10

Browse other questions tagged or ask your own question.