Correct reading of comment lines in input file using Fortran 90

I understand that when reading data from a file, Fortran will skip lines beginning with and an asterisk (*), assuming that they are a comment. Well, it looks like I'm having a problem achieving this behavior with the very simple program I created. This is my simple Fortran program:

  1       program test
  2 
  3       integer dat1
  4 
  5       open(unit=1,file="file.inp")
  6 
  7       read(1,*) dat1
  8 
  9 
 10       end program test

This is the file.inp file:

  1 *Hello
  2 1

I built my simple program with

gfortran -g -o test test.f90

When I run, I get an error message:

At line 7 of file test.f90 (unit = 1, file = 'file.inp')
Fortran runtime error: Bad integer for item 1 in list input

When I run the input file with the comment line deleted, that is:

1 1

The code is working fine. So the problem seems to be that Fortran correctly interprets this comment line. It should be something extremely simple, I'm missing here, but I can't add anything to Google.

+5
2

Fortran . , , , , , " " .

- :

use, intrinsic :: iso_fortran_env

character (len=200) :: line
integer :: dat1, RetCode

read_loop: do
   read (1, '(A)', isostat=RetCode)  line
    if ( RetCode == iostat_end)  exit ReadLoop
    if ( RetCode /= 0 ) then
      ... read error
      exit ReadLoop
    end if
    if ( index (line, "*") /= 0 )  cycle read_loop
    read (line, *) dat1
end do read_loop
+7

Fortran , , .

0

All Articles