# 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.

OPEN ([UNIT=] io-unit[, FILE= name] [, ERR= label] [, IOMSG=msg-var] [, 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 FILE Specifier.
label | Is the label of the branch target statement that receives control if an error occurs. For more information, see Branch Specifiers.
msg-var | Is a scalar default character variable that is assigned an explanatory message if an I/O error occurs. For more information, see I/O Message Specifier.
i-var | Is a scalar integer variable that is defined as a positive integer (the number of the error message) if an error occurs, a negative integer if an end-of-file record is encountered, and zero if no error occurs. For more information, see I/O Status Specifier.
slist | Is one or more of the following OPEN specifiers in the form specifier= value or specifier (each specifier can appear only once):
ACCESS | DECIMAL | NAME | ROUND
ACTION | DEFAULTFILE | NEWUNIT | SHARE
ASSOCIATEVARIABLE | DELIM | NOSHARED | SHARED
ASYNCHRONOUS | DISPOSE | ORGANIZATION | SIGN
BLANK | ENCODING | PAD | STATUS
BLOCKSIZE | FILE | POSITION | TITLE
BUFFERCOUNT | FORM | READONLY | TYPE
BUFFERED | IOFOCUS | RECL | USEROPEN
CARRIAGECONTROL | MAXREC | RECORDSIZE |
CONVERT | MODE | RECORDTYPE |
The OPEN specifiers and their acceptable values are summarized in the OPEN Statement Overview. The control specifiers that can be specified in an OPEN statement are discussed in I/O Control List in the Language Reference.

## Description

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.

If you specify BLANK=, DECIMAL=, PAD=, ROUND=, or SIGN=, you must also specify FMT= or NML=.

If you specify ID=, you must also specify ASYNCHRONOUS='YES'.

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.

If the NEWUNIT= specifier does not appear, an io-unit must be specified. If the keyword UNIT= is omitted, the io-unit must be first in the control list.

If the NEWUNIT= specifier appears, an io-unit must not be specified.

If the NEWUNIT= specifier appears, either the FILE= specifier or the STATUS=SCRATCH specifier must also appear.

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.

## Example

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*6 FINAL /' '/
      ...
      IF (expr) FINAL = 'DELETE'
      OPEN (UNIT=1, STATUS='NEW', DISP=FINAL)

The following statement creates a new sequential formatted file on unit 1 with the default file name fort.1:

      OPEN (UNIT=1, STATUS='NEW', ERR=100)

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

       OPEN (unit=10, DEFAULTFILE='/usr/users/someone/', FILE='test.dat',
      1     FORM='FORMATTED', STATUS='OLD')

The following example opens a new file:

     ! Prompt user for a filename and read it:
     CHARACTER*64 filename
     WRITE (*, '(A\)') ' enter file to create: '
     READ (*, '(A)') filename
     ! Open the file for formatted sequential access as unit 7.
     ! Note that the specified access need not have been specified,
     ! since it is the default (as is "formatted").
     OPEN (7, FILE = filename, ACCESS = 'SEQUENTIAL', STATUS = 'NEW')

The following example opens an existing file called DATA3.TXT:

     ! Open a file created by an editor, "DATA3.TXT", as unit 3:
     OPEN (3, FILE = 'DATA3.TXT')

Parent topic: O to P

## See Also

READ
WRITE
CLOSE
FORMAT
INQUIRE
OPEN Statement Overview