Reading C data file in F90

I am not an expert in programming, but I have some experience. More than a week has passed when I try to read a data file from C into a Fortran program. Program C stores the matrix in a bin-format data file as follows:

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

and my reading section in F90:

 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 help me solve the problem.

+4
source share
2 answers

Based on a fairly detailed listening to Fortran IO , I think you misunderstand "unformatted." Unformatted does not mean binary, it just means separator text. Your C program certainly does not write delimiters. The easiest solution, if you can change the C code, is to use fprintf instead of fwrite and arrange the format to fit Fortran IO's expectations. If you cannot, I recommend writing another C program to read the output of the existing one and writing some data compatible with fortran.

+1
source

if you have an option, use netcdf or hdf5 instead.

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

but check your details. It seems you are writing a vector to a file, but you seem to be reading a matrix from another file

+1
source

Source: https://habr.com/ru/post/1314236/


All Articles