[comp.lang.fortran] Bug cured by debugging output.

davidp@dbrmelb.dbrhi.oz (David Paterson) (01/10/90)

I've been programming in Fortran 77 for ten years now and have seen
all sorts of things but this is one of the strangest.

The program below works perfectly when run with:
Microsoft FORTRAN77 V3.31 August 1985           and
Microsoft (R) 8086 Object Linker Version 3.04.


      CHARACTER*80 LINE
      DATA XLO,XHI,YLO,YHI/0.,10.,0.,10./
 49   WRITE(*,50) XLO,XHI,YLO,YHI
 50   FORMAT(' Type XLO,XHI,YLO,YHI <',3(G9.3,','),G9.3,'>: ')
      READ(*,'(A80)') LINE
      WRITE(*,*) 
      READ(LINE,*,END=49,ERR=49) XLO,XHI,YLO,YHI
      STOP
      END

However, if the line 'WRITE(*,*)' is removed then the program
fails miserably (endless loop). Why?

-------------------------------------------------------------------
David Paterson
CSIRO Division of Building, Construction and Engineering
Highett, Victoria, AUSTRALIA

mckenney@ACF8.NYU.EDU (Alan Michael McKenney) (01/12/90)

Subject: Re: Bug cured by debugging output.
Newsgroups: comp.lang.fortran
In-Reply-To: article <711@dbrmelb.dbrhi.oz> of 9 Jan 90 22:29 EST

In article <711@dbrmelb.dbrhi.oz>, davidp@dbrmelb.dbrhi.oz (David Paterson)
writes:

# I've been programming in Fortran 77 for ten years now and have seen
# all sorts of things but this is one of the strangest.
# 
# The program below works perfectly ....
...
# 
# 
#       CHARACTER*80 LINE
#       DATA XLO,XHI,YLO,YHI/0.,10.,0.,10./
#  49   WRITE(*,50) XLO,XHI,YLO,YHI
#  50   FORMAT(' Type XLO,XHI,YLO,YHI <',3(G9.3,','),G9.3,'>: ')
#       READ(*,'(A80)') LINE
#       WRITE(*,*) 
#       READ(LINE,*,END=49,ERR=49) XLO,XHI,YLO,YHI
#       STOP
#       END
# 
# However, if the line 'WRITE(*,*)' is removed then the program
# fails miserably (endless loop). Why?
# 
# -------------------------------------------------------------------
# David Paterson
# CSIRO Division of Building, Construction and Engineering
# Highett, Victoria, AUSTRALIA

    The legalistic answer to why your program doesn't work: it violates
the FORTRAN-77 standard: Par 12.2.5.2 says

    Internal File Restrictions. ...

    (1) Reading and writing records is accomplished only by
        sequential access formateed input/output statements
	(12.8.1) that do not specify list-directed formatting.

I can't give a good practical justification for this; the Unix
compilers we use allow it, but my (limited) experience with
microcomputer compilers suggests that you are lucky to have it even run
standard-conforming programs correctly.

    Since it isn't legal FORTRAN 77, it isn't a bug for a compiler/
run-time library to do strange things with it.  Unless the manual for
Microsoft FORTRAN says it allows this as an extension, you are out of
luck.  A user-friendly compiler would flag this usage as an error, or
try to make it behave in some intuitively obvious way.  I don't know if
the END or ERR exit is Microsoft's way of flagging the illegal code, or
if it is just an unplanned result of feeding the I/O library arguments
it doesn't expect; I suspect the latter, since the "WRITE(*,*)" has an
effect on it.


Alan McKenney        E-mail:  mckenney@robalo.nyu.edu          (INTERNET)
Courant Institute,NYU         ...!cmcl2!robalo!mckenney        (UUCP)