I am currently trying to C++ read a Fortran-written binary file, and I am not having much success. The Fortran code that writes the file is not my own, although the C++ parsing routine is.
The first record of the binary file has been written using the following statement(s):
INTEGER var1 var2 var3
WRITE(12,REC=1) var1,var2,var3
A Fortran snippet that performs a succcesfull read looks like this:
open(unit=10,file="ETC.bin",access='direct',recl=24,iostat=iost,status='old')
read (unit=10,rec=1) var1,var2,var3
close(unit=10)
print*,var1,var2,var3
On the C++ side of things, I have so far come up with the following:
FILE* binfile = fopen("ETC.bin","rb") ;
fseek (binfile,0,SEEK_END) ;
long lSize = ftell (binfile) ;
char* buffer = (char*) malloc (sizeof(char)*lSize) ;
rewind (binfile) ;
size_t result=fread(buffer,1,96,binfile) ;
for (unsigned i = 0; i<=result; i++){
printf("%f\n",buffer[i]) ;
}
My C++ printf statement, unfortunately, returns nonsense. Note that I am assuming that Fortran is relying on 4 bit words (e.g. gfortran compiler), and that if ifort is used, the
--assume byterecl
option is needed at compile time.
I know what the result should be, but I am not sure as to how to duplicate the behavior of the Fortran read statements in C++.
Thanks for any and all help!
P.S. There is a similar question posted here: reading fortran binary file in c++, which points to the following dead link. Not much information out there, or my Google-Fu is lousy.