FORTRAN 77 Language Reference

OPEN

The OPEN statement can connect an existing external file to a unit, create a file and connect it to a unit, or change some specifiers of the connection.

OPEN ([UNIT=] u, slist)

Parameter 

Description 

UNIT

Unit number 

slist

The specifiers list slist can include one or more of the following

  • FILE = fin or alternativly NAME = fin

  • ACCESS = acc

  • BLANK = blnk

  • ERR = s

  • FORM = fm

  • IOSTAT = ios

  • RECL = rl or alternativly RECORDSIZE = rl

  • STATUS = sta or alternativly TYPE = sta

  • FILEOPT = fopt @

  • READONLY @

  • ACTION = act @

Description

The OPEN statement determines the type of file named, whether the connection specified is legal for the file type (for instance, DIRECT access is illegal for tape and tty devices), and allocates buffers for the connection if the file is on tape or if the subparameter FILEOPT= 'BUFFER= n' is specified. Existing files are never truncated on opening.


Note -

For tape I/O, use the TOPEN() routines.


The following table summarizes the OPEN specifiers:

Table 4-3 OPEN Specifiers Summary

Form: SPECIFIER = Variable 

SPECIFIER 

Value of Variable  

Data Type of Variable 

ACCESS

'APPEND' 'DIRECT' 'SEQUENTIAL'

CHARACTER

ACTION

'READ' 'WRITE' 'READWRITE'

CHARACTER

BLANK

'NULL'

'ZERO'

CHARACTER

ERR

Statement number  

INTEGER

FORM

'FORMATTED' 'UNFORMATTED' 'PRINT'

CHARACTER

FILE *

Filename or '*'

CHARACTER

FILEOPT

'NOPAT' 'BUFFER=n' 'EOF'

CHARACTER

IOSTAT

Error number 

INTEGER

READONLY

RECL

Record length 

INTEGER

STATUS

'OLD' 'NEW' 'UNKNOWN' 'SCRATCH'

CHARACTER

The keywords can be specified in any order.

OPEN Specifier Keywords

The following provides a detailed list of the OPEN specifier keywords:

[UNIT=] u

FILE=fin

If you open a unit that is already open without specifying a file name (or with the previous file name), FORTRAN thinks you are reopening the file to change parameters. The file position is not changed. The only parameters you are allowed to change are BLANK (NULL or ZERO) and FORM (FORMATTED or PRINT). To change any other parameters, you must close, then reopen the file.

If you open a unit that is already open, but you specify a different file name, it is as if you closed with the old file name before the open.

If you open a file that is already open, but you specify a different unit, that is an error. This error is not detected by the ERR= option, however, and the program does not terminate abnormally.

If a file is opened with STATUS='SCRATCH', a temporary file is created and opened. See STATUS=sta.

ACCESS=acc

FORM=fm

RECL=rl

ERR=s

IOSTAT=ios

BLANK=blnk

STATUS=sta

FILEOPT=fopt @

READONLY @

ACTION=act

Examples

Here are six examples.

Example 1: Open a file and connect it to unit 8--either of the following forms of the OPEN statement opens the file, projectA/data.test, and connects it to FORTRAN unit 8:


       OPEN(UNIT=8, FILE='projectA/data.test') 
       OPEN(8, FILE='projectA/data.test') 

In the above example, these properties are established by default: sequential access, formatted file, and (unwisely) no allowance for error during file open.

Example 2: Explicitly specify properties:


       OPEN(UNIT=8, FILE='projectA/data.test', &        ACCESS='SEQUENTIAL', FORM='FORMATTED')

Example 3: Either of these opens file, fort.8, and connects it to unit 8:


       OPEN(UNIT=8) 
       OPEN(8)

In the above example, you get sequential access, formatted file, and no allowance for error during file open. If the file, fort.8 does not exist before execution, it is created. The file remains after termination.

Example 4: Allowing for open errors:


       OPEN(UNIT=8, FILE='projectA/data.test', ERR=99)

The above statement branches to 99 if an error occurs during OPEN.

Example 5: Allowing for variable-length records;


       OPEN(1, ACCESS='DIRECT', recl=1)

See "Direct Access I/O" for more information about variable-length records.

Example 6: Scratch file:


       OPEN(1, STATUS='SCRATCH')

This statement opens a temporary file with a name, such as tmp.FAAAa003zU. The file is usually in the current working directory, or in TMPDIR if that environment variable is set.