FIX: Record Not Advanced If Branching to ERR= on READ

Last reviewed: September 16, 1997
Article ID: Q75757
5.00 5.10 | 5.00 5.10
MS-DOS    | OS/2
kbprg kbfixlist kbbuglist

The information in this article applies to:

  • Microsoft FORTRAN for MS-DOS, versions 5.0 and 5.1
  • Microsoft FORTRAN for OS/2, versions 5.0 and 5.1

SYMPTOMS

Unexpected or incorrect results may be generated while reading a file.

CAUSE

When an error is encountered on a READ statement containing an ERR=label field, the program correctly branches to the error label, and the file pointer remains positioned after the point where the error occurred. Upon reading the file again, if any data remains on that record, the READ statement will fill variables with values from that record. Although this practice may at times appear unintuitive, the ANSI standard is unclear about where the file pointer should be positioned following an error branching on the READ statement.

However, if no variables remain on a record in which an error is branched to upon a prior READ statement, when reading from the file again, the next variable is filled with a zero. See the sample code for more information.

RESOLUTION

Force the file pointer to advance to the next record.

STATUS

Microsoft has confirmed this to be a problem in Microsoft FORTRAN versions 5.0 and 5.1. This problem was corrected in FORTRAN PowerStation, version 1.0.

MORE INFORMATION

The following code can be used to reproduce the problem with a test.dat of

   1
   2
   3
   a
   5
   6
   7
   8
   9
   10


Sample Code #1

       open(1,file='test.dat')
       do 10 j=1,10
         read(1,'(i3)',err=100,end=200) i
         print*,i
10 continue
       stop
100 print*,'read error'
       goto 10
200 end

The above code produces the following results:

          1
          2
          3
read error
          0                  ! Not advancing to next record at this point
          5
          6
          7
          8
          9
         10

The following code forces the file pointer to advance to the next record when an error is encountered during a READ statement:

Sample Code #2

          open(1,file='test.dat')
          do 10 j=1,10
            read(1,'(i3)',err=100,end=200) i
            print*,i
   10     continue
          stop
  100     print*,'read error'
          read(1,*) ! advance to next record
          goto 10
  200     end

The output for the above code is as follows:

          1
          2
          3
read error
          5
          6
          7
          8
          9
         10


Additional reference words: 5.00 5.10 buglist5.00 buglist5.10 fixlist1.00
KBCategory: kbprg kbfixlist kbbuglist
KBSubCategory: FORTLngIss
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 16, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.