FIX: Read-Only UNFORMATTED Files Hang the Machine (77505)



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

This article was previously published under Q77505

SYMPTOMS

If a program compiled under Microsoft FORTRAN version 5.0 or 5.1 writes to a file that has the READ-ONLY attribute set and is OPENed FORM='UNFORMATTED', the run-time system may return the following message:
run-time error F6421: CLOSE(Test.DAT)
- File READ-ONLY or locked against writing.
This message may be displayed several times under MS-DOS before the machine finally hangs. Under OS/2, the same error message may be displayed several times before a protection violation is generated. Sample code 1 below illustrates this problem.

When attempting to read from a non-existent file OPENed with MODE='READ' and FORM='UNFORMATTED', this same error message may be generated several times and the machine may hang. In OS/2, a protection violation may be generated after the message is displayed several times. Sample code 2 below illustrates this problem.

If a file is OPENed MODE='READ' and the program attempts to WRITE to that file, the same run-time error F6421 is generated several times and then the machine may hang. Under OS/2, the same message may be generated several times before producing a protection violation. Sample code 3 below illustrates this problem.

CAUSE

Normally, if the run-time system performs an OPEN statement, it first tries to open the file with the READWRITE attribute; if it fails, it will try again with MODE='READ'. If the run-time system fails again, it tries to open the file with MODE='WRITE'. If it fails each time, it will generate a run-time error or it will run the instructions in the line specified in the ERR label.

In the case of the UNFORMATTED file, the OPEN statement is not checking the file attributes unless the file is OPENed in a specific mode. The OPEN command is ignoring the ERR label unless the MODE is set to READWRITE, READ, or WRITE. Writing to READ-ONLY DIRECT access, BINARY, and FORMATTED files does not produce this problem.

RESOLUTION

A solution to this problem is to set the MODE attribute in the OPEN statement for unformatted files. If the user is not sure of the present status of the file to be accessed, the status can be requested using the INQUIRE statement before attempting to open the desired file.

However, when OPENing a file with MODE='READ' in addition to FORM='UNFORMATTED' and then attempting to WRITE to that file or READ from a non-existent file, the same run-time error F6421 and machine hang or protection violation will be generated. There is no known work-around for this problem at this time.

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

Sample Code #1

The following code generates the F6421 error before hanging the machine or producing a protection violation if the file TEST.DAT is marked with a READ-ONLY attribute:
        OPEN (1, FILE='Test.DAT',  FORM='UNFORMATTED')
        WRITE (1) 'Example of the Problem'
        END
				
Solution:
        OPEN (1, FILE='Test.DAT',  FORM='UNFORMATTED',
           &    MODE='READWRITE')
        WRITE (1) 'Example of the Problem'
        END
				

Sample Code #2

The following code generates the F6421 error before hanging the machine or producing a protection violation if the file TEST.DAT does not exist:
        OPEN (1, FILE='Test.DAT', MODE='READ', FORM='UNFORMATTED')
        READ (1)  variable
        END
				

Sample Code #3

The following code generates the F6421 error before hanging the machine or producing a protection violation:
        OPEN (1, FILE='Test.DAT', MODE='READ', FORM='UNFORMATTED')
        WRITE (1) 'Sample Output'
        END
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:KB77505