Using the IOSTAT Specifier and Fortran Exit Codes

You can use the IOSTAT specifier to continue program execution after an I/O error and to return information about I/O operations. As described in Visual Fortran Run-Time Errors, certain errors are not returned in IOSTAT.

Although the IOSTAT specifier transfers control, it can only return information returned by the Visual Fortran RTL. For additional error handling capabilities, see the GERROR and PERROR routines.

The IOSTAT specifier can supplement or replace the END, EOR, and ERR branch transfers.

Execution of an I/O statement containing the IOSTAT specifier suppresses the display of an error message and defines the specified integer variable, array element, or scalar field reference as one of the following, which is returned as an exit code if the program terminates:

Following the execution of the I/O statement and assignment of an IOSTAT value, control transfers to the END, EOR, or ERR statement label, if any. If there is no control transfer, normal execution continues. For more information on transfer of control, see Branch Statements.

You can include the iosdef.for file in your program to obtain symbolic definitions for the values of IOSTAT.

The following example uses the IOSTAT specifier and the iosdef.for file to handle an OPEN statement error (in the FILE specifier).

Error Handling OPEN Statement File Name

     CHARACTER(LEN=40) :: FILNM
     INCLUDE 'iosdef.for'

     DO I=1,4
       FILNM = ''
       WRITE (6,*)  'Type file name '
       READ (5,*) FILNM
       OPEN (UNIT=1, FILE=FILNM, STATUS='OLD', IOSTAT=IERR, ERR=100)
       WRITE (6,*) 'Opening file: ', FILNM
!       (process the input file)
       CLOSE (UNIT=1)
       STOP

  100  IF (IERR .EQ. FOR$IOS_FILNOTFOU) THEN
         WRITE (6,*) 'File: ', FILNM, ' does not exist '
       ELSE IF (IERR .EQ. FOR$IOS_FILNAMSPE) THEN
          WRITE (6,*) 'File: ', FILNM, ' was bad, enter new file name'
       ELSE
         PRINT *, 'Unrecoverable error, code =', IERR
         STOP
       END IF
     END DO
     WRITE (6,*) 'File not found. Locate correct file with Explorer and run again'
   END PROGRAM

Within the DO loop, instead of the READ statement that assigns a value to variable FILNM, you could instead call the GetOpenFileName Win32 routine, which requests the file name using a dialog box. For an example of how to call the GetOpenFileName routine, see the Visual Fortran Sample GetOpenFileName in ...Df98\Samples\Advanced\Win32\GetOpenFileName.