9.6 OPEN Statement

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] . . . )
par
Is a keyword specification taking one of the following forms:
keywd
keywd = val
keywd
Is any of the following keywords (listed in categories based on function):
val
Is a value that is valid for the keyword. Table 9-1 lists these values.

Table 9-1 OPEN Statement Keyword Values on OpenVMS Systems

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  Error transfer label   
EXTENDSIZE   e  File allocation increment   Volume or system default 
FILE
NAME
 
File specification (file name)   FORnnn.DAT[1] 
FORM  'FORMATTED'
'UNFORMATTED'  
Format type  Depends on ACCESS keyword 
INITIALSIZE   e  File allocation   
IOSTAT  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
 
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  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.


Note
You can use the INQUIRE statement to get file attributes of existing files. For more information, see Section 9.5.

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)

Examples

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.


Previous Page Next Page Table of Contents