BUG: Implied Do-loop Ignores END=Label in READ at End of File (72919)



The information in this article applies to:

  • Microsoft FORTRAN Compiler for MS-DOS 5.0
  • Microsoft FORTRAN Compiler for MS-DOS 5.1
  • Microsoft FORTRAN compiler for OS/2 5.0
  • Microsoft FORTRAN compiler for OS/2 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS 1.0
  • Microsoft FORTRAN PowerStation for MS-DOS 1.0a
  • Microsoft Fortran Powerstation 32 for Windows NT 1.0
  • Microsoft Fortran Powerstation 32 for Windows NT 4.0

This article was previously published under Q72919

SYMPTOMS

A program compiled with Microsoft FORTRAN, that uses an implied DO-loop in a READ statement that has the END=label or ERR=label directive, will not branch to the specified label if the loop reads past the end of the file. The implied DO-loop will continue to completion with no error and without branching to the end label. The elements of the array that cannot be read from the file are filled with zeros.

CAUSE

There is no way to trap the end-of-file condition in the middle of reading an implied DO-loop.

RESOLUTION

To obtain predictable results when using implied DO-loops, ensure that there is sufficient data in each record to fill all the elements of the array that is being read into. The END=label specifier cannot be relied upon to trap insufficient data on the final record.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

This is not a problem with the run-time handling of the END= or ERR= directives in READ statements. When an implied DO-loop reads past the amount of data on a record, the default run-time end-of-record handling fills the remaining array elements with zeros. This occurs even on the last record in a file. The end of file is not encountered when an implied DO-loop runs out of data on the final record in a file. The end-of-record condition is met first, and the remainder of the array is filled with zeros without allowing the end of file to be read. If another READ statement occurred at this point, the end of file would be encountered.

The following program creates a data file with only 7 elements on a single record. It then rewinds the file and reads 50 elements from this single record. Neither the END= label nor the ERR= label is branched to, and the 43 extra records are filled with zeros.
      program test
      real*4 arr(50)

c Fill the array with numbers from 1 to 50.

      do 10,j=1,50
      arr(j) = float(j)
10    continue

      open(8,file='junkk')

c Write only 7 elements to the single record in the file.

      write(8,100) (arr(i), i=1,7)

      rewind(8)

c Read 50 elements from the file.

      read(8,200,end=300,err=250) (arr(i),i=1,50)

      write(*,*) arr

100   format(7f10.2)
200   format(50f10.2)

c Branch to the end to avoid error messages.

      stop 'No branch to error or end labels'

250   write(*,*) 'got to error label'
300   write(*,*) 'got to the end of file label'

      end
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbLangFortran KB72919