Opening Files: OPEN Statement

To open a file, you should use a preconnected file (such as for terminal output) or explicitly open files with an OPEN statement. Although you can also implicitly open a file, this prevents you from using the OPEN statement to specify the file connection characteristics and other information.

OPEN Statement Specifiers

The OPEN statement connects a unit number with an external file and allows you to explicitly specify file attributes and run-time options using OPEN statement specifiers. Once you open a file, you should close it before opening it again unless it is a preconnected file.

If you open a unit number that was opened previously (without being closed), one of the following occurs:

You can use the INQUIRE statement to obtain information about whether or not a file is opened by your program.

Especially when creating a new file using the OPEN statement, examine the defaults (see the description of the OPEN statement in the Intel Fortran Language Reference Manual) or explicitly specify file attributes with the appropriate OPEN statement specifiers.

Specifiers for File and Unit Information

These specifiers identify file and unit information:

Specifiers for File and Record Characteristics

These specifiers identify file and record characteristics:

Specifier for Special File Open Routine  

USEROPEN names the routine that will open the file to establish special context that changes the effect of subsequent Intel Fortran I/O statements.

Specifiers for File Access, Processing, and Position

These specifiers identify file access, processing, and position:

Specifiers for Record Transfer Characteristics

These specifiers identify record transfer characteristics:

Specifiers for Error-Handling Capabilities

These specifiers are used for error handling:

Specifier for File Close Action  

DISPOSE identifies the action to take when the file is closed.

Coding File Locations in an OPEN Statement

You can use the FILE and DEFAULTFILE specifiers of the OPEN statement to specify the complete definition of a particular file to be opened on a logical unit. (The Language Reference Manual describes the OPEN statement in greater detail.)

For example:

OPEN (UNIT=4, FILE='/usr/users/smith/test.dat', STATUS='OLD')

The file test.dat in directory /usr/users/smith is opened on logical unit 4. No defaults are applied, because both the directory and file name were specified. The value of the FILE specifier can be a character constant, variable, or expression.

In the following interactive example, the user supplies the file name and the DEFAULTFILE specifier supplies the default values for the full pathname string. The file to be opened is in /usr/users/smith and is concatenated with the file name typed by the user into the variable DOC:


WRITE (6,*)  'Type file name '

READ (5,*) DOC


A slash is appended to the end of the default file string if it does not have one.