# 12.6 OPEN Statement

The OPEN statement connects an external file to a unit, creates a new file and connects it to a unit, creates a preconnected file, or changes certain properties of a connection.

The OPEN statement takes the following form:

  OPEN ([UNIT=]io-unit [, FILE=name] [, ERR=label] [, IOSTAT=i-var], slist)
  io-unit
  Is an external unit specifier.
  name
  Is a character or numeric expression specifying the name of the file to be connected. For more information, see Section 12.6.14.
  label
  Is the label of the branch target statement that receives control if an error occurs.
  i-var
  Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.
  slist
  Is one or more OPEN specifiers in the form specifier = value or specifier. Each specifier can appear only once.

  The OPEN specifiers and their acceptable values are summarized in Table 12-1.

  The OPEN specifiers are described individually in the following sections. The control specifiers that can be specified in an OPEN statement (UNIT, ERR, and IOSTAT) are discussed in Section 10.2.1.

Table 12-1 OPEN Statement Specifiers and Values on OpenVMS Systems

Specifier | Values | Function | Default
--- | --- | --- | ---
ACCESS | 'SEQUENTIAL' 'DIRECT' 'KEYED' 'APPEND' | Access mode | 'SEQUENTIAL'
ACTION | 'READ' 'WRITE' 'READWRITE' | File access | 'READWRITE'
ASSOCIATEVARIABLE | var | Next direct access record | No default
BLANK | 'NULL' 'ZERO' | Interpretation of blanks | 'NULL'
BLOCKSIZE | n_expr | Physical block size | System default
BUFFERCOUNT | n_expr | Number of I/O buffers | System default
BUFFERED | 'YES' 'NO' | Buffering for WRITE operations | 'NO'
CARRIAGECONTROL | 'FORTRAN' 'LIST' 'NONE' | Print control | Formatted: 'FORTRAN' Unformatted: 'NONE'
CONVERT | 'LITTLE_ENDIAN' 'BIG_ENDIAN' 'CRAY' 'FDX' 'FGX' 'IBM' 'VAXD' 'VAXG' 'NATIVE' | Numeric format specification | 'NATIVE'
DEFAULTFILE | c_expr | Default file specification | Current working directory
DELIM | 'APOSTROPHE' 'QUOTE' 'NONE' | Delimiter for character constants | 'NONE'
DISPOSE (or DISP) | 'KEEP' or 'SAVE' 'DELETE' 'PRINT' 'PRINT/DELETE' 'SUBMIT' 'SUBMIT/DELETE' | File disposition at close | 'KEEP'
ERR | label | Error transfer control | No default
EXTENDSIZE | n_expr | File allocation increment | Volume or system default
FILE (or NAME) | c_expr | File specification (file name) | FORnnn.DAT 1
FORM | 'FORMATTED' 'UNFORMATTED' | Format type | Depends on ACCESS setting
INITIALSIZE | n_expr | File allocation | No default
IOSTAT | var | I/O status | No default
KEY | (e1:e2[:dt[:dr]],...) | Key field definitions | CHARACTER ASCENDING
MAXREC | n_expr | Direct access record limit | No limit
NOSPANBLOCKS | No value | Records do not span blocks | No default
ORGANIZATION | 'SEQUENTIAL' 'RELATIVE' 'INDEXED' | File organization | 'SEQUENTIAL'
PAD | 'YES' 'NO' | Record padding | 'YES'
POSITION | 'ASIS' 'REWIND' 'APPEND' | File positioning | 'ASIS'
READONLY | No value | Write protection | No default
RECL (or RECORDSIZE) | n_expr | Record length | Depends on RECORDTYPE, ORGANIZATION, and FORM settings
RECORDTYPE | 'FIXED' 'VARIABLE' 'SEGMENTED' 'STREAM' 'STREAM_CR' 'STREAM_LF' | Record type | Depends on ORGANIZATION, ACCESS, and FORM settings
SHARED | No value | File sharing allowed | No default 2
STATUS (or TYPE) | 'OLD' 'NEW' 'SCRATCH' 'REPLACE' 'UNKNOWN' | File status at open | 'UNKNOWN' 3
UNIT | n_expr | Logical unit number | No default; an io-unit must be specified
USEROPEN | func | User program option | No default
1 nnn is the unit number (with leading zeros, if necessary) 2 For information on file sharing, see your user manual. 3 The default differs under certain conditions (see Section 12.6.29).
Key to Values
       c_expr: A scalar default character expression
       dr:     A direction, ASCENDING or DESCENDING
       dt:     A data type, INTEGER or CHARACTER
       e1:     The first byte position of a key
       e2:     The last byte position of a key
       func:   An external function
       label:  A statement label
       n_expr: A scalar numeric expression
       var:    A scalar integer variable

Rules and Behavior

The control specifiers ([UNIT=]io-unit, ERR=label, and IOSTAT=i-var) and OPEN specifiers can appear anywhere within the parentheses following OPEN. However, if the UNIT specifier is omitted, the io-unit must appear first in the list.

Specifier values that are scalar numeric expressions 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.

Only one unit at a time can be connected to a file, but multiple OPENs can be performed on the same unit. If an OPEN statement is executed for a unit that already exists, the following occurs:

- If FILE is not specified, or FILE specifies the same file name that appeared in a previous OPEN statement, the current file remains connected.

  If the file names are the same, the values for the BLANK, CARRIAGECONTROL, CONVERT, DELIM, DISPOSE, ERR, IOSTAT, and PAD specifiers can be changed. Other OPEN specifier values cannot be changed, and the file position is unaffected.

- If FILE specifies a different file name, the previous file is closed and the new file is connected to the unit.

The ERR and IOSTAT specifiers from any previously executed OPEN statement have no effect on any currently executing OPEN statement. If an error occurs, no file is opened or created.

Secondary operating system messages do not display when IOSTAT is specified. To display these messages, remove IOSTAT or use a platform-specific method. (For more information, see your user manual.)

Examples

You can specify character values at run time by substituting a character expression for a specifier value in the OPEN statement. The character value can contain trailing blanks but not leading or embedded blanks; 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',              &
           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 specifier 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')

For More Information:

- On Fortran IOSTAT errors, see your user manual.
- On the UNIT control specifier, see Section 10.2.1.1.
- On the ERR control specifier, see Section 10.2.1.8.
- On the IOSTAT control specifier, see Section 10.2.1.7.
- On using the INQUIRE statement to get file attributes of existing files, see Section 12.5.
- On OPEN statements and file connection, see your user manual.

Previous Page Next Page Table of Contents