The CLOSE statement terminates the connection between a specified input/output unit number and an external file.
Syntax
CLOSE ( close-spec-list )Where:close-spec-list is a comma-separated list of close-specs.
close-specs are:
[UNIT =]external-file-unit,
[IOSTAT=iostat],
[ERR=label],
[STATUS=status]
external-file-unit is the input/output unit number of an external file.
iostat is a scalar default INTEGER variable. It signals either success or failure after execution of the CLOSE statement.
label is the label of a branch target statement to which the program branches if there is an error in executing the CLOSE statement.
status is a CHARACTER expression that evaluates to either 'KEEP' or 'DELETE'.
The CLOSE statement must specify an external-file-unit.
If UNIT=is omitted, external-file-unit must be the first specifier in close-spec-list.
If IOSTAT=iostat is present, iostat has a value of zero if the unit was successfully closed. If an error occurs when executing the CLOSE statement, iostat is assigned an error number which identifies which error occurred.
A specifier must not appear more than once in a CLOSE statement.
STATUS='KEEP' must not be specified for a file whose status is 'SCRATCH'. If 'KEEP' is specified for a file that exists, the file continues to exist after a CLOSE statement. This is the default behavior.
If STATUS='DELETE' is specified, the file associated with the unit number will be deleted upon execution of the CLOSE statement.
Example 1
integer :: ios close(8,iostat=ios,status='DELETE') if(ios == 0) then write(*,*) " No error occurred." else write(*,*) " IOSTAT= encourages structured programming." end ifExample 2
close(unit=8,err=200) write(*,*) " No error occurred." goto 300 200 write(*,*) " An error occurred" 300 continue