tags:

views:

59

answers:

2

Hi,

I am not an expert in programming but have some experience. It is more than a week that I am trying to read a data file from C into a Fortran program. C program saves a matrix in a bin format data file as follow:

FILE * amatFile;
amatFile = fopen("A.dat","wb");
for(krowa=0;krowa<N2;krowa++){ 
    fwrite(amat[krowa], sizeof(float), S2, amatFile); 
    }
fclose(amatFile);

and my read section in F90 is:

open(unit=1,file='AMAT.dat',form='unformatted')
    DO i = 1,M
            Do j = 1,N
        READ(unit=1) AMAT(i,j)
        A(i,j) = AMAT(i,j)
        End do
    End Do
close(1)

I really appreciate if you can help me to solve the problem.

+1  A: 

if you have option, consider using netcdf or hdf5 instead.

fortran io is major pain. http://local.wasp.uwa.edu.au/~pbourke/dataformats/fortran/

but do check your input. You seem to be writing vector to file, but you seem to be reading matrix from a different file

aaa
+1  A: 

Based on a rather detailed writup of Fortran IO, I think you are misunderstanding 'unformatted'. Unformatted doesn't mean binary, it just means delimited text. Your C program is surely not writing delimiters. The easiest solution, if you can change the C code, is to use fprintf instead of fwrite, and arrange the format to match Fortran IO's expectations. If you can't, then I recommend writing another C program to read the output of the existing one and write some fortran-compatible data.

bmargulies