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.
Syntax
The OPEN specifiers and their acceptable values are summarized in the OPEN Statement in the Language Reference.
The control specifiers that can be specified in an OPEN statement are discussed in I/O Control List in the Language Reference.
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 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.
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.
Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB
See Also: READ, WRITE, CLOSE, FORMAT, INQUIRE, OPEN Statement
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*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 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', &
RECL=1024)
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')