The IOSTAT specifier can be used to continue program execution after an I/O error, or an end-of-file or end-of-record condition occurs, and to return information about the status of I/O operations. Certain error conditions are not returned in IOSTAT.
The IOSTAT specifier can supplement or replace the use of the END=, EOR=, and ERR= branch specifiers.
Use of an IOSTAT= specifier in an I/O statement prevents initiation of error termination if an error occurs during the execution of the I/O statement. The integer variable specified in the IOSTAT= specifier becomes defined during the execution of the I/O statement with the following values:
0 for normal completion of the I/O statement (no error, end-of-file, or end-of-record condition occurs).
The value of the (negative) default integer scalar constant IOSTAT_EOR defined in the intrinsic module ISO_FORTRAN_ENV if no error condition or end-of-file condition occurs, but an end-of-record condition does occur during the execution of an input statement.
The value of the (negative) default integer scalar constant IOSTAT_END defined in ISO_FORTRAN_ENV if no error condition occurs, but an end-of-file condition does occur during the execution of an input statement.
For an INQUIRE statement, the value of the default integer constant IOSTAT_INQUIRE_INTERNAL_UNIT defined in ISO_FORTRAN_ENV if a file-unit-number identifies an internal unit in the execution of the statement.
A positive integer value if an error condition occurs. (This value is one of the Fortran-specific IOSTAT numbers listed in the run-time error message. See List of Run-Time Error Messages, which lists the messages.)
Note that the value assigned to the IOSTAT variable is the same value that would be returned as an exit code if error termination was initiated.
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.
The non-standard include file FOR_ISODEF.FOR and the non-standard module FORISODEF contain symbolic constants for the values returned through an IOSTAT= specifier.
The following example uses the IOSTAT specifier and the module FORIOSDEF to handle an OPEN statement error (in the FILE specifier).
USE foriosdef
IMPLICIT NONE
CHARACTER(LEN=40) :: FILNM
INTEGER IERR
PRINT *, 'Type file name:'
READ (*,*) FILNM
OPEN (UNIT=1, FILE=FILNM, STATUS='OLD', IOSTAT=IERR, ERR=100)
PRINT *, 'Opening file: ', FILNM
! process the input file
...
CLOSE (UNIT=1)
STOP
100 IF(IERR . EQ. FOR$IOS_FILNOTFOU) THEN
PRINT *, 'File: ', FILNM, ' does not exist '
ELSE
PRINT *, 'Unrecoverable error, code =', IERR
END IF
END PROGRAM
Another way to obtain information about an error is the ERRSNS subroutine, which allows you to obtain the last I/O system error code associated with an Intel® Fortran RTL error (see the Intel® Fortran Language Reference).