CLOSE

Purpose

The CLOSE statement disconnects an external file from a unit.

Syntax

Read syntax diagramSkip visual syntax diagram>>-CLOSE--(--close_list--)-------------------------------------><
 
close_list
is a list that must contain one unit specifier (UNIT=u) and can also contain one of each of the other valid specifiers. The valid specifiers are:
[UNIT=] u
is a unit specifier in which u must be an external unit identifier whose value is not an asterisk. An external unit identifier refers to an external file that is represented by an integer expression, whose value is in the range 1 through 2147483647. If the optional characters UNIT= are omitted, u must be the first item in close_list.
ERR= stmt_label
is an error specifier that specifies the statement label of an executable statement in the same scoping unit to which control is to transfer in the case of an error. Coding the ERR= specifier suppresses error messages.
Fortran 2003 Standard
IOMSG= iomsg_variable
is an input/output status specifier that specifies the message returned by the input/output operation. iomsg_variable is a scalar default character variable. It must not be a use-associated nonpointer protected variable. When the input/output statement containing this specifier finishes execution, iomsg_variable is defined as follows:
  • If an error, end-of-file, or end-of-record condition occurs, the variable is assigned an explanatory message as if by assignment.
  • If no such condition occurs, the value of the variable is unchanged.
End of Fortran 2003 Standard
IOSTAT= ios
is an input/output status specifier that specifies the status of the input/output operation. ios is an integer variable. When the input/output statement containing this specifier finishes executing, ios is defined with:
  • A zero value if no error condition occurs
  • A positive value if an error occurs.
STATUS= char_expr
specifies the status of the file after it is closed. char_expr is a scalar character expression whose value, when any trailing blanks are removed, is either KEEP or DELETE.
  • If KEEP is specified for a file that exists, the file will continue to exist after the CLOSE statement. If KEEP is specified for a file that does not exist, the file will not exist after the CLOSE statement. KEEP must not be specified for a file whose status prior to executing the CLOSE statement is SCRATCH.
  • If DELETE is specified, the file will not exist after the CLOSE statement.

The default is DELETE if the file status is SCRATCH; otherwise, the default is KEEP.

Rules

A CLOSE statement that refers to a unit can occur in any program unit of an executable program and need not occur in the same scoping unit as the OPEN statement referring to that unit. You can specify a unit that does not exist or has no file connected; the CLOSE statement has no effect in this case.

IBM Extension BeginsUnit 0 cannot be closed. IBM Extension Ends

When an executable program stops for reasons other than an error condition, all units that are connected are closed. Each unit is closed with the status KEEP unless the file status prior to completion was SCRATCH, in which case the unit is closed with the status DELETE. The effect is as though a CLOSE statement without a STATUS= specifier were executed on each connected unit.

If a preconnected unit is disconnected by a CLOSE statement, the rules of implicit opening apply if the unit is later specified in a WRITE statement (without having been explicitly opened).

Examples

CLOSE(15)
CLOSE(UNIT=16,STATUS='DELETE')

Related information