Using backspace when reading an unformatted file after a read error does not
work except if it is on the first record.

The code below show the problem. This code write 3 couples of integer values in
an unformatted file called 'foo' and close it.
File is reopen for read.
Then it reads the first couple of values. It's OK.
Then it try to read 3 values and detects the error (only 2 values in the
record).
The code use backspace to go back at the begining of this record and try to
read again this record, but with only 2 values.
The values obtained are wrong.

Trying to read 3 values at the begining of the file, using backspace and
reading again only 2 values works. 

Code:

      PROGRAM gfort
      IMPLICIT NONE

      INTEGER :: x, z, count
      INTEGER :: nx, nz,LocalTimeStep 


      OPEN (21,FILE='foo',form='UNFORMATTED',err=101)
      REWIND(21)
      x=140
      z=170
      WRITE(21) x,z
      WRITE(6,*)'writing x (',x,') and z(',z,') in foo'
      x=14
      z=17
      WRITE(21) x,z
      WRITE(6,*)'writing x (',x,') and z(',z,') in foo'
      x=1400
      z=1700
      WRITE(21) x,z
      WRITE(6,*)'writing x (',x,') and z(',z,') in foo'
      CLOSE(21)


      OPEN (21,FILE='foo',form='UNFORMATTED',status='OLD',err=101)
      REWIND(21)
      WRITE(6,*)'reading first x, z in foo'
      x=-1
      z=-1
      READ(21,err=103,end=103) x,z
      WRITE(6,*)'x (',x,') and z(',z,')'

      WRITE(6,*)'reading x, z and LocalTimeStep in foo'
      nx=-1
      nz=-1
      READ(21,iostat=count) nx,nz,LocalTimeStep
      WRITE(6,*)'x (',nx,') and z(',nz,') and LocalTimeStep(',
     + LocalTimeStep,')'
         IF (count.NE.0) THEN
            WRITE(6,*)'reading again x and z in foo'
            BACKSPACE(21)
            nx=-1
            nz=-1
            READ(21,err=103,end=103) nx,nz
            LocalTimeStep=0.0
      WRITE(6,*)'x (',nx,') and z(',nz,') and LocalTimeStep(',
     + LocalTimeStep,')'
         ENDIF
      CLOSE(21)
      STOP 'OK'

101   WRITE(6,*) 'foo n''a pas ete trouve.'
      STOP 'FAILED'
103   WRITE(6,*) 'Erreur de lecture'
      CLOSE(21)
      STOP 'FAILED'
      END PROGRAM gfort



NORMAL RESULT:
 ./a.out
 writing x (         140 ) and z(         170 ) in foo
 writing x (          14 ) and z(          17 ) in foo
 writing x (        1400 ) and z(        1700 ) in foo
 reading first x, z in foo
 x (         140 ) and z(         170 )
 reading x, z and LocalTimeStep in foo
 x (          14 ) and z(          17 ) and LocalTimeStep(           0 )
 reading again x and z in foo
 x (          14 ) and z(          17 ) and LocalTimeStep(           0 )
OK

Gfortran RESULT (WRONG) :
./a.out
 writing x (         140 ) and z(         170 ) in foo
 writing x (          14 ) and z(          17 ) in foo
 writing x (        1400 ) and z(        1700 ) in foo
 reading first x, z in foo
 x (         140 ) and z(         170 )
 reading x, z and LocalTimeStep in foo
 x (          14 ) and z(          17 ) and LocalTimeStep(          17 )
 reading again x and z in foo
 x (          17 ) and z(           8 ) and LocalTimeStep(           0 )
STOP OK


-- 
           Summary: backspace intrinsic is not working on an unformatted
                    file
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Patrick dot Begou at hmg dot inpg dot fr
 GCC build triplet: gcc version 4.3.0 20070418 (experimental)
GCC target triplet: i386-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31618

Reply via email to