Pages

jeudi 1 mars 2012

end of file for a file created by linux command and fortran

If we type:
echo "a b c d " > tt

Then emacs tt, we will find that the end of file is not in the line of "a b c d", instead in the next line.

It is the same when we save the results of grep or cat with the symbol >

If we read such a file with fortran, we found that the number of lines in the file is 1 line more than the actual line number (from wc -l) !!  We need pay attention to this difference if we want to read the file using fortran.

If we read the file by fortran:
allocate(data(realsize))
iEof = 0
i = 1
DO while ( iEof == 0)
     read(iu, *, IOSTAT=iEof)  temp_data
     data(i) = temp_datga
     i = i + 1
Enddo
close(iu)

This would cause memory problem:
* glibc detected free(): invalid next size (normal): 0x000000000103dd90 **. 
Invalid next size" means that glibc has detected corruption in the memory arena.


This is because : data is allocated with realsize, but when we read the file, there are one more line (the last line is blank) is read and the blank line overwrites the good information already stored to the variable "data". This cause the problem !!! The corrected code should be as follows:



allocate(data(realsize))
i = 1
DO while ( i <= realsize)
     read(iu, *, IOSTAT=iEof)  temp_data
     data(i) = temp_datga
     i = i + 1
Enddo
close(iu)

Aucun commentaire:

Enregistrer un commentaire