13.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)

Is an external unit specifier.

Is a character or numeric expression specifying the name of the file to be connected. For more information, see Section 13.6.13.

Is the label of the branch target statement that receives control if an error occurs.

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

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 13-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 13-1 OPEN Statement Specifiers and Values on Tru64 UNIX, Linux, and Windows Systems

Specifier  Values  Function  Default 
Access mode  'SEQUENTIAL'  
(or MODE 1)  
File access  'READWRITE'  
ASSOCIATEVARIABLE  var   Next direct access record  No default 
Interpretation of blanks  'NULL'  
BLOCKSIZE  n_expr   Physical block size  Filesystem default 
BUFFERCOUNT  n_expr   Number of I/O buffers  One 
Buffering for WRITE operations  'NO' 
Print control  Formatted: 'LIST' 2
Unformatted: 'NONE'
Numeric format specification   'NATIVE'  
DEFAULTFILE  c_expr  Default file pathname  Current working directory 
Delimiter for character constants  'NONE' 
(or DISP)
'KEEP' or 'SAVE'
File disposition at close   'KEEP'  
ERR  label  Error transfer control  No default 
(or NAME)  
c_expr  File pathname (file name)   fort.n 3 
'BINARY' 1  
Format type  Depends on ACCESS setting 
IOFOCUS1   .TRUE. .FALSE.  Active window in QuickWin application  .TRUE. 4 
IOSTAT  var  I/O status  No default 
MAXREC  n_expr  Direct access record limit  No limit 
File organization   'SEQUENTIAL'  
Record padding  'YES' 
File positioning  'ASIS' 
READONLY  No value  Write protection  No default 
n_expr   Record length  Depends on RECORDTYPE, ORGANIZATION, and FORM settings 5 
Record type  Depends on ORGANIZATION, CARRIAGECONTROL, ACCESS, and FORM settings 
File locking   'DENYWR'  
SHARED 6  No value  File sharing allowed  U*X: SHARED
WNT AND W9*: No default 
File status at open   'UNKNOWN' 7 
TITLE 1  c_expr  Title for child window in QuickWin application  No default 
UNIT  n_expr  Logical unit number  No default; an io-unit must be specified 
USEROPEN  func   User program option  No default 
1 WNT, W9*
2 If you use the compiler option specifying OpenVMS defaults, and the unit is connected to a terminal, the default is 'FORTRAN'.
3 n is the unit number.
4 If you specify unit '*' the default is .FALSE..
5 On Tru64 UNIX and Linux systems, the default depends only on the FORM setting.
6 For information on file sharing, see your user manual or programmer's guide.
7 The default differs under certain conditions (see Section 13.6.28).
Key to Values
   c_expr: A scalar default character expression
   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 keyword 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:

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 or programmer's guide.)


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:

  IF (expr) FINAL = 'DELETE'
The following statement creates a new sequential formatted file on unit 1 with the default file name fort.1:
The following statement creates a file on magnetic tape:
OPEN (UNIT=I, FILE='/dev/rmt8',                                  &
     STATUS='NEW', ERR=14, RECL=1024)

The following statement opens the file (created in the previous example) for input:

OPEN (UNIT=I, FILE='/dev/rmt8', READONLY, STATUS='OLD',          &

The following example opens the existing file /usr/users/someone/test.dat:

 OPEN (unit=10, DEFAULTFILE='/usr/users/someone/', FILE='test.dat',

For More Information:

Previous Page Next Page Table of Contents