Apparent problem with list-directed reads and trailing comma

John McFarland mcfarljm@gmail.com
Sat Nov 9 00:27:00 GMT 2013


Below is a program with an example input file that contains 3 lines.  On 
Windows with gfortran 4.7.2, I get this error:
At line 6 of file prog.f90
Fortran runtime error: Bad integer for item 1 in list input

The error is related to the comma at the end of the first line and goes 
away if I remove it (or, interestingly, if I change the line endings to 
Unix line endings, even though I am testing on Windows). What actually 
happens is instead of reading the 2nd line, the 2nd read reads from a 
"phantom" empty line (I've identified this from other debugging not 
shown here), so the third read is trying to read from the second line, 
which doesn't have numerical data.

This appears to be a bug, although maybe I am missing something?

Thanks,
John


PROGRAM prog
   INTEGER :: x, y, unit
   OPEN(newunit=unit, file='input.txt', status='old')
   READ(unit,*) x
   READ(unit,*) ! Skip line
   READ(unit,*) y
   CLOSE(unit)
END PROGRAM prog

$ cat input.txt
1,
skip this line
2



More information about the Fortran mailing list