The OPEN statement connects an existing file to a logical unit or creates a new file and connects it to a logical unit. It can also specify file attributes that control file creation and subsequent processing. The OPEN statement takes the following form:
OPEN (par [,par] . . . )
keywd keywd = val
DEFAULTFILE | Default file specification |
DISPOSE | File existence status after CLOSE |
FILE or NAME | File specification (file name) |
STATUS or TYPE | File existence status at OPEN |
UNIT | Logical unit number to be used |
ACCESS | Fortran access method to be used |
ORGANIZATION | Logical file structure |
READONLY | Write protection |
BLANK | Blank interpretation for numeric input |
BLOCKSIZE | Physical block size |
CARRIAGECONTROL | Printer control type |
CONVERT | Numeric format for unformatted data |
FORM | Type of Fortran record formatting |
KEY | Positions of key fields within records in an indexed file |
RECL or RECORDSIZE | Logical record length |
RECORDTYPE | Logical record format |
EXTENDSIZE | File allocation increment size |
INITIALSIZE | Initial file allocation |
ASSOCIATEVARIABLE | The next record number value |
MAXREC | Maximum direct access record number |
BUFFERCOUNT | Number of I/O buffers to be used |
ERR | Statement to which control is transferred if an error occurs during execution of the OPEN statement |
IOSTAT | Status value that indicates whether an error condition exists |
NOSPANBLOCKS | Records are not to be split across physical blocks |
SHARED | Other programs can simultaneously access the file |
USEROPEN | User program option to provide additional OPEN capability |
Keyword | Values | Function | Default |
---|---|---|---|
ACCESS | 'SEQUENTIAL'
'DIRECT' 'KEYED' 'APPEND' |
Access mode | 'SEQUENTIAL' |
ASSOCIATEVARIABLE | asv | Next direct access record | |
BLANK | 'NULL'
'ZERO' | Interpretation of blanks | ' NULL' |
BLOCKSIZE | e | Physical block size | System default |
BUFFERCOUNT | e | Number of I/O buffers | System default |
CARRIAGECONTROL | 'FORTRAN'
'LIST' 'NONE' |
Print control | 'FORTRAN'
(formatted)
'NONE' (unformatted) |
CONVERT | 'LITTLE_ENDIAN'
'BIG_ENDIAN' 'CRAY' 'FDX' 'FGX' 'IBM' 'VAXD' 'VAXG' 'NATIVE' |
Numeric format specification | 'NATIVE' |
DEFAULTFILE | c1 | Default file specification | |
DISPOSE
DISP |
'KEEP' or 'SAVE'
'DELETE' 'PRINT' 'PRINT/DELETE' 'SUBMIT' 'SUBMIT/DELETE' |
File disposition at close | 'KEEP' |
ERR | s | Error transfer label | |
EXTENDSIZE | e | File allocation increment | Volume or system default |
FILE
NAME | c | File specification (file name) | FORnnn.DAT[1] |
FORM | 'FORMATTED'
'UNFORMATTED' | Format type | Depends on ACCESS keyword |
INITIALSIZE | e | File allocation | |
IOSTAT | v | I/O status | |
KEY | (e1:e2[:dt[:dr]],...) | Key field definitions | CHARACTER
ASCENDING |
MAXREC | e | Direct access record limit | |
NOSPANBLOCKS | - | Records do not span blocks | |
ORGANIZATION | 'SEQUENTIAL'
'RELATIVE' 'INDEXED' |
File structure | 'SEQUENTIAL' |
READONLY | - | Write protection | |
RECL
RECORDSIZE | e | Record length | Depends on record type and file organization |
RECORDTYPE | 'FIXED'
'VARIABLE' 'SEGMENTED' 'STREAM' 'STREAM_CR' 'STREAM_LF' |
Record structure | Depends on ORGANIZATION, ACCESS, and FORM keywords |
SHARED | - | File sharing allowed | |
STATUS
TYPE | 'OLD'
'NEW' 'SCRATCH' 'UNKNOWN' | File status at open | 'UNKNOWN' |
UNIT | e | Logical unit number | |
USEROPEN | p | User program option | |
[1] n is the unit number (with leading zeros, if necessary) | |||
Key to Values
asv: An integer variable c : A character scalar reference, numeric scalar memory reference, or numeric array name reference c1 : A character expression dr : A direction, ASCENDING or DESCENDING dt : A data type, INTEGER or CHARACTER e : A numeric expression e1 : The first byte position of a key e2 : The last byte position of a key p : An external function s : A statement label v : An integer scalar memory reference |
If an OPEN statement is executed for a unit that is already open, and the file specification is different from that of the current open file, the previously opened file is closed and the new file is opened. If the file specification is the same for both files, the new value of the BLANK= specifier is in effect, but the position of the file is unaffected.
Specifying OPEN Statement Keywords
Keyword specifications can appear in any order. In most cases, they are optional. Default values apply in their absence. If the logical unit specifier is the first parameter in the list, the UNIT keyword is optional.
You can specify character values at run time by substituting a general character expression for a keyword value in the OPEN statement. The character value can contain trailing spaces but not leading or embedded spaces. For example:
CHARACTER*7 QUAL /' '/ . . . IF (exp) QUAL = '/DELETE' OPEN (UNIT=1, STATUS='NEW', DISP='SUBMIT'//QUAL)
The following statement creates a new sequential formatted file on unit 1 with the default file name FOR001.DAT:
OPEN (UNIT=1, STATUS='NEW', ERR=100)
The following statement creates a 50-block direct access file for temporary storage. The file is deleted at program termination.
OPEN (UNIT=3, STATUS='SCRATCH', ACCESS='DIRECT', 1 INITIALSIZE=50, RECL=64)
The following statement creates a file on magnetic tape with a large block size for efficient processing:
OPEN (UNIT=I, FILE='MTA0:MYDATA.DAT', BLOCKSIZE=8192, 1 STATUS='NEW', ERR=14, RECL=1024, 1 RECORDTYPE='FIXED')
The following statement opens the file (created in the previous example) for input:
OPEN (UNIT=I, FILE='MTA0:MYDATA.DAT', READONLY, 1 STATUS='OLD', RECL=1024, RECORDTYPE='FIXED', 1 BLOCKSIZE=8192)
The following statement uses the file name supplied by the user and the default file specification supplied by the DEFAULTFILE keyword to define the file specification for an existing file:
TYPE *, 'ENTER NAME OF DOCUMENT' ACCEPT *, DOC OPEN (UNIT=1, FILE=DOC, DEFAULTFILE='[ARCHIVE].TXT', 1 STATUS='OLD')
The following sections provide specific information about OPEN statement keywords. As used in these sections, a numeric expression can be any integer or real expression. The value of the expression is converted to integer data type before it is used in the OPEN statement.