strange behavior on end-of-record in READ

Mark Rashid mmrashid@ucdavis.edu
Wed Oct 19 20:35:00 GMT 2011


Greetings fortran list,

Consider the following test code:


      program test
!
      open (unit = 4, file = 'test.i', status = 'old')
!
      read (4, *, iostat = ios) i1, i2, i3, i4
      write (6, 100) i1, i2, i3, i4, ios
      read (4, *, iostat = ios) i1, i2, i3, i4
      write (6, 100) i1, i2, i3, i4, ios
  100 format (1x, 4(i3, 2x), i6)
!
      stop
      end


If we make file test.i this:

1  2  3  4
5  6  7  8

. . . then we get the expected/correct output, which is:

   1    2    3    4       0
   5    6    7    8       0

But changing test.i to:

1  2  3  
5  6  7  8

. . . produces this as output:

   1    2    3    5       0
   1    2    3    5      -1

It appears that the first READ is "wrapping around" and reading the
first data item from the second line in the input file, and without
throwing a nonzero IOSTAT.  Stranger still, the second line of output
suggests that the file is being repositioned, following the first READ,
to ahead of the *first* line of input.  This might be understandable if
the first READ were generating an error (in which case the file position
is indeterminate), but we have that zero IOSTAT on the first READ.  I
can't make any sense of the -1 for IOSTAT on the second READ.

I can't see how this behavior is intended, but perhaps?  The standard
says that the things that constitute an "error" on READ are
processor-dependent.

I'm using gfortran 4.7 from May 9, 2011.

Would be grateful for an illumination.







More information about the Fortran mailing list