Intel® Fortran Compiler 16.0 User and Reference Guide
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.
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:
If you specify a file specification that does not match the one specified for the original open, the Intel Fortran run-time system closes the original file and then opens the second file. This 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 lets you change one or more OPEN statement run-time specifiers while maintaining the record position context.
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.
These specifiers identify file and unit information:
UNIT specifies the logical unit number.
NEWUNIT specifies that the Intel® Fortran run-time 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.
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.
USEROPEN names the routine that will open the file to establish special context that changes the effect of subsequent Intel Fortran I/O statements.
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.
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.
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.
DISPOSE identifies the action to take when the file is closed.
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.