# How to import a binary data generated by Fortran? [closed]

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

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

Program Test
use output
implicit none
integer::c(4)=[1,2,3,4]
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

Block[{
filename3 = "Code/c.dat",
l},
SetDirectory[NotebookDirectory[]];
str = OpenRead[filename3, BinaryFormat -> True];
l = BinaryReadList[str, "Integer32", 4, ByteOrdering -> \$ByteOrdering];
Close[filename3];
]


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:

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

## closed as off-topic by MarcoB, m_goldberg, xzczd, Carl Lange, Alex TrounevMar 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.

• 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. – Szabolcs Mar 4 at 18:03
• Could you give an example of the data - the first few lines, perhaps? – Carl Lange Mar 4 at 18:54
• Please post the data as text rather than as an image. – Carl Lange Mar 4 at 19:13
• 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] – chuy Mar 4 at 20:42
• 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. – finalboss Mar 4 at 21:10