FORTRAN 77 Language Reference


The CLOSE statement disconnects a file from a unit.

CLOSE([UNIT=] u [, STATUS= sta] [, IOSTAT= ios] [, ERR= s])




Unit identifier for an external unit. If UNIT= is not used, then u must be first.


Determines the disposition of the file--sta is a character expression whose value, when trailing blanks are removed, can be KEEP or DELETE. The default value for the status specifier is KEEP. For temporary (scratch) files, sta is forced to DELETE always. For other files besides scratch files, the default sta is KEEP.


I/O status specifier--ios must be an integer variable or an integer array element.


Error specifier--s must be the label of an executable statement in the same program containing the CLOSE statement. The program control is transferred to this statement in case an error occurs while executing the CLOSE statement.


The options can be specified in any order.

The DISP= and DISPOSE= options are allowable alternates for STATUS=, with a warning, if the -ansi flag is set.

Execution of CLOSE proceeds as follows:

  1. The specified unit is disconnected.

  2. If sta is DELETE, the file connected to the specified unit is deleted.

  3. If an IOSTAT argument is specified, ios is set to zero if no error was encountered; otherwise, it is set to a positive value.


All open files are closed with default sta at normal program termination. Regardless of the specified sta, scratch files, when closed, are always deleted.

Execution of a CLOSE statement specifying a unit that does not exist, or a unit that has no file connected to it, has no effect.

Execution of a CLOSE statement specifying a unit zero (standard error) is not allowed, but you can reopen it to some other file.

The unit or file disconnected by the execution of a CLOSE statement can be connected again to the same, or a different, file or unit.

Note -

For tape I/O, use the TOPEN() routines.


Example 1: Close and keep:

       CLOSE ( 2, STATUS='KEEP') 

Example 2: Close and delete:


Example 3: Close and keep a scratch file even though the status is SCRATCH:

       OPEN ( 2, STATUS='SCRATCH')