Close

Statement Purpose

The CLOSE statement disconnects an external file from a program I/O unit number.

Valid Specifiers

There are several specifiers that can be included in a CLOSE statement. A complete list of all of them and their definitions follow below.

unit=

Out of all the specifiers that can be used in a close statement, this is the only on that must appear. The "unit=" followed by a program unit number, or just the unit number, has to be present so that the computer knows what file to disconnect from the program. The unit number used should have been associated with an external file through the use of the open statement earlier in the program.

iostat=

If used, this specifier will store an integer value in an integer variable placed after the equals sign. The number stored in the variable will correspond to the status of the close operation. If an error occurred while attempting to close the file a positive value will be stored in the variable, while a value of zero will be stored in the variable if no error occurred. If an error did occur, the meaning behind the exact positive value that is stored inside the iostat variable is totally processor dependent. Therefore, to find out the meaning of an error condition you'll have to dig out the Fortran manual for your particular system.

err=

This specifier gives the statement label to which the program should branch to if an error occurred while attempting to close a unit.

status=

Finally, this specifier will dictate what will happen to the external file after it is closed. One of two character expressions are placed after this specifier. First, if the character expression 'KEEP' is used, the external file will continue to exist after it is closed. Second, if 'DELETE' is placed after the equals sign, then the file will be erased after it is closed. By default, the status on all files is KEEP when they are closed, except for files that were open with the file status SCRATCH. Also, since files that were opened with the file status SCRATCH are not meant to exist after the execution is finished, these files can never have KEEP specified when they are closed.

Rules

There are a few rules that apply when using the close statement. First, you can specify a unit that was never connected to be closed. In this case the close statement will have no effect. Also, a file that was opened previously with the status SCRATCH can not be closed with the status KEEP. It must be closed with the status DELETE. Another thing that a programmer should keep in mind is that a file that was opened in one program unit does not need to be closed in that same program unit. In other words, if a file was opened in the main program, it could be closed inside a subroutine without any problems. Finally, files do not need to be closed before execution of the program ceases. Any files that remain open when the program is finished are automatically closed.

Examples

The close statement usually appears in its simplest form, with only the unit number accompanying the close statement.
		close(37)
For a list of specifiers that the close argument can support read the section on valid specifiers.

For additional examples and information see

lectures thirteen and twenty two

examples: curvefit.f and plot2.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu