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

ID 767251
Date 7/13/2023
Public

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

Document Table of Contents

Open Files: OPEN Statement

To open a file, you can use a preconnected file (as described in Logical Devices) or can open a file with an OPEN statement. The OPEN statement lets you 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 runtime 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:

  • If you specify a file specification that does not match the one specified for the original open, the runtime system closes the original file and then opens the second file. This action resets the current record position for the second file.

  • If you specify a file specification that matches the one specified for the original open, the file is reconnected without the internal equivalent of the CLOSE and OPEN. This behavior lets you change one or more OPEN statement runtime specifiers while maintaining the record position context.

You can use the INQUIRE Statement to obtain information about whether 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) or explicitly specify file attributes with the appropriate OPEN statement specifiers.

Specifiers for File and Unit Information

These specifiers identify file and unit information:

  • UNIT specifies the logical unit number.

  • NEWUNIT specifies that the Intel® Fortran runtime library should select an unused logical unit number.

  • FILE (or NAME) and DEFAULTFILE specify the directory and/or file name of an external file.

  • STATUS or TYPE indicates whether to create a new file, overwrite an existing file, open an existing file, or use a scratch file.

  • STATUS or DISPOSE specifies the file existence status after CLOSE.

Specifiers for File and Record Characteristics

These specifiers identify file and record characteristics:

  • ORGANIZATION indicates the file organization (sequential or relative).

  • RECORDTYPE indicates which record type to use.

  • FORM indicates whether records are formatted or unformatted.

  • CARRIAGECONTROL indicates the terminal control type.

  • RECL or RECORDSIZE specifies the record size.

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:

  • ACCESS indicates the access mode (direct, sequential, or stream).

  • SHARED sets file locking for shared access. Indicates that other users can access the same file.

  • NOSHARED sets file locking for exclusive access. Indicates that other users who use file locking mechanisms cannot access the same file.

  • SHARE specifies shared or exclusive access; for example, SHARE='DENYNONE' or SHARE='DENYRW'.

  • POSITION indicates whether to position the file at the beginning of file, before the end-of-file record, or leave it as is (unchanged).

  • ACTION or READONLY indicates whether statements will be used to only read records, only write records, or both read and write records.

  • MAXREC specifies the maximum record number for direct access.

  • ASSOCIATEVARIABLE specifies the variable containing the next record number for direct access.

  • ASYNCHRONOUS specifies whether input/output should be performed asynchronously.

Specifiers for Record Transfer Characteristics

These specifiers identify record transfer characteristics:

  • BLANK indicates whether to ignore blanks in numeric fields.

  • DELIM specifies the delimiter character for character constants in list-directed or namelist output.

  • PAD, when reading formatted records, indicates whether padding characters should be added if the item list and format specification require more data than the record contains.

  • BUFFERED indicates whether buffered or non-buffered I/O should be used.

  • BLOCKSIZE specifies the physical I/O buffer or transfer size.

  • BUFFERCOUNT specifies the number of physical I/O buffers.

  • CONVERT specifies the format of unformatted numeric data.

Specifiers for Error-Handling Capabilities

These specifiers are used for error handling:

  • ERR specifies a label to branch to if an error occurs.

  • IOSTAT specifies the integer variable to receive the error (IOSTAT) number if an error occurs.

Specifier for File Close Action

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

Specifying 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 located in /usr/users/smith and is concatenated with the file name typed by the user into the variable DOC:

CHARACTER(LEN=9) DOC
WRITE (6,*)  'Type file name '
READ (5,*) DOC
OPEN (UNIT=2, FILE=DOC, DEFAULTFILE='/usr/users/smith',STATUS='OLD') 

A slash (backslash on Windows systems) is appended to the end of the default file string if it does not have one.

On Windows, there is a default limit of 260 characters in a pathname string. Longer strings are truncated to that limit. Windows provides an override of the limit. To use the override, the pathname must be absolute (from the drive), not relative (from the current directory). To override, start the pathname with "\\?\". If DEFAULTFILE is set, it can be prefixed with the override. Fortran supports this override for the FILE and DEFAULTFILE specifiers, up to a limit of 4095 characters.

On Linux, the pathname limit is always 4095 characters.