Terminates file connection.
Syntax
CLOSE ( io-specifier-list ) |
 |
- io-specifier-list
is a list of the following
comma-separated I/O specifiers:
- [UNIT=]unit
specifies the
unit connected to an external file. unit must be a positive integer-valued expression. If
the optional keyword UNIT= is omitted, unit must be the first item in io-specifier-list.
- ERR=stmt-label
specifies the
label of the executable statement to which control passes if an error
occurs during statement execution. If neither IOSTAT= or ERR= is specified and an error occurs, the program
aborts and a system error message is issued. stmt-label must be in the same scoping unit as the CLOSE statement with the ERR= specifier.
- IOSTAT=integer-variable
returns the I/O
status after the statement executes. If the statement executes successfully, integer-variable is set to zero. If an error occurs, it is set to
a positive integer that indicates which error occurred. If neither IOSTAT= or ERR= is specified and an error occurs, the program
aborts and a system error message is issued.
- STATUS=character-expression
specifies the
state of the file after it is closed. character-expression can be one of the following arguments:
- 'KEEP'
Preserve the file after it is closed (default).
- 'DELETE'
Do not preserve the file after it is closed.
The STATUS= specifier is ignored if the file was opened as
a scratch file. See “OPEN” for a description of the OPEN statement.
Description
The CLOSE statement closes the file whose unit number was
obtained from an OPEN statement. A CLOSE statement must contain a unit number and at most
one each of the other I/O specifiers.
A CLOSE statement need not be in the same program unit
as the OPEN statement that connected the file to the specified
unit. If a CLOSE statement specifies a unit that does not exist
or has no file connected to it, no action occurs.
Examples
The following examples illustrate different uses of the CLOSE statement. In the first example, the CLOSE statement closes the file connected to unit 10;
after it is closed, the file will continue to exist, unless it was
opened with the STATUS='SCRATCH' specifier:
In the next example, after the file connected to unit 6 is
closed, it will cease to exist:
CLOSE(UNIT=6,STATUS='DELETE') |
The following code produces the same results as the previous
example:
CHARACTER(LEN=6) cstat cstat='delete' CLOSE(UNIT=6,STATUS=cstat) |
The following example closes the file connected to unit 5.
If an error occurs, control is transferred to the executable statement
labeled 100, and the error code is stored in the variable ios:
CLOSE(5,IOSTAT=ios,ERR=100) |
Related statements
OPEN
Related concepts
For information about I/O concepts, see Chapter 8 “I/O
and file handling”, which also lists example programs that
use I/O.