Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

CLOSE

Statement: Disconnects a file from a unit.

CLOSE ( [UNIT=] io-unit[, {STATUS | DISPOSE | DISP} = p] [, ERR= label] [, IOMSG=msg-var] [, IOSTAT=i-var])

io-unit

(Input) an external unit specifier.

p

(Input) a scalar default character expression indicating the status of the file after it is closed. It has one of the following values:

  • 'KEEP' or 'SAVE' - Retains the file after the unit closes.

  • 'DELETE' - Deletes the file after the unit closes (unless OPEN(READONLY) is in effect).

  • 'PRINT' - Submits the file to the line print spooler, then retains it (sequential files only).

  • 'PRINT/DELETE' - Submits the file to the line print spooler, then deletes it (sequential files only).

  • 'SUBMIT' - Forks a process to execute the file.

  • 'SUBMIT/DELETE' - Forks a process to execute the file, then deletes the file after the fork is completed.

The default is 'DELETE' for user windows in Windows* QuickWin applications and for scratch files. For all other files, the default is 'KEEP'.

Scratch files are temporary and are always deleted upon normal program termination; specifying STATUS='KEEP' for scratch files causes a runtime error.

For user windows in Windows* QuickWin applications, STATUS='KEEP' causes the child window to remain on the screen even after the unit closes. The default status is 'DELETE', which removes the child window from the screen.

label

Is the label of the branch target statement that receives control if an error occurs.

msg-var

(Output) Is a scalar default character variable that is assigned an explanatory message if an I/O error occurs.

i-var

(Output) Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

Description

The CLOSE statement specifiers can appear in any order. An I/O unit must be specified, but the UNIT= keyword is optional if the unit specifier is the first item in the I/O control list.

The status specified in the CLOSE statement supersedes the status specified in the OPEN statement, except that a file opened as a scratch file cannot be saved, printed, or submitted, and a file opened for read-only access cannot be deleted.

If a CLOSE statement is specified for a unit that is not open, it has no effect.

You do not need to explicitly close open files. Normal program termination closes each file according to its default status. The CLOSE statement does not have to appear in the same program unit that opened the file.

Closing unit 0 automatically reconnects unit 0 to the keyboard and screen. Closing units 5 and 6 automatically reconnects those units to the keyboard or screen, respectively. Closing the asterisk (*) unit causes a compile-time error. In Windows QuickWin applications, use CLOSE with unit 0, 5, or 6 to close the default window. If all of these units have been detached from the console (through an explicit OPEN), you must close one of these units beforehand to reestablish its connection with the console. You can then close the reconnect unit to close the default window.

If a parameter of the CLOSE statement is an expression that calls a function, that function must not cause an I/O operation or the EOF intrinsic function to be executed, because the results are unpredictable.

NOTE:

You may get unexpected results if you specify OPEN with a filename and a USEROPEN specifier that opens a different filename, and then use a CLOSE statement with STATUS=DELETE (or DISPOSE=DELETE). In this case, the runtime library assumes you want to delete the file named in the OPEN statement, not the one you specified in the USEROPEN function. For more information about how to use the USEROPEN specifier, see User-Supplied OPEN Procedures: USEROPEN Specifier.

Example

!    Close and discard file:
     CLOSE (7, STATUS = 'DELETE')

Consider the following statement:

  CLOSE (UNIT=J, STATUS='DELETE', ERR=99)

This statement closes the file connected to unit J and deletes it. If an error occurs, control is transferred to the statement labeled 99.