OPEN

OPEN

It is possible to open a file dynamically (that is, at run time) by means of the Fortran OPEN statement.

The OPEN statement will cause a file to become connected. It is used to describe the properties of a connection in addition to performing the connection itself. For example, in order to open a text file for input, the following statement might appear in a program:

OPEN(UNIT=8,FILE='FRED')

It will be apparent that the name of the file and a unit number are used together with some defaults provided by the system in order to open the file.

The general form of the OPEN statement is:

OPEN(<olist>)

<olist> is a list of specifiers. It must contain exactly one external unit specifier <unit> and may contain, at most, one of each of the other specifiers listed below:

UNIT=<value>
<value> is the unit number. The keyword and equals sign are optional if this is the first item in the list. UNIT values should be greater than 6 to avoid the possibility of re-assigning standard input/output streams.

FILE=<filename>
<filename> is the name of the file to which the unit will be connected when it is opened. This is only optional if the specifier STATUS=SCRATCH is present.

IOSTAT=<variable>
<variable> is an integer numeric variable into which the input/output status will be written. The variable will be positive if an error occurred, otherwise it will be zero.

ERR=<label>
<label> is a label to jump to in the event of an error.

ACCESS=<string>
This specifies the type of access for which the file is to be opened. <string> is either DIRECT, SEQUENTIAL or STREAM. As an extension to the Fortran standard <string> can also be TRANSPARENT (see the FORM specifier below). By default the ACCESS is SEQUENTIAL.
STREAM is part of the Fortran 2003 Standard and is similar to FTN95 TRANSPARENT access. When compared with DIRECT, STREAM uses POS in place of REC and has a fixed RECL value which for FTN95 is one. The Fortran 2003 intrinsic NEW_ LINE can be used within an WRITE statement. It returns a CHARACTER result which for FTN95 is CHAR(10) and is translated to CHAR(13) then CHAR(10) in the output file.

FORM=<string>
<string>is a character expression (typically a constant), whose value is FORMATTED, UNFORMATTED or PRINTER when any trailing blanks are removed.
FORM specifies whether the data transfer to and from the program will be formatted, unformatted or in line printer format.
If the specifier is omitted, FORMATTED is assumed if the file is being connected for sequential access and UNFORMATTED is assumed if the file is being connected for direct or transparent access (see the ACCESS specifier above).
FORM=PRINTER is an extension to the ANSI Standard, and specifies that the first column of any output record is taken as a Fortran carriage control character.
The Fortran carriage control characters are as follows:

Character

Vertical Spacing before printing

Blank

One line

0

Two lines

1

To first line of next page

+

No advance

Carriage return, linefeed, and form feed control characters are output as necessary to give the effects above.
Note that FORM=PRINTER is only appropriate for files on which output only is performed.
For ACCESS = TRANSPARENT and FORM= FORMATTED, no carriage returns are output at the end of record on output (the user can output carriage returns with the "/" editing descriptor), and on input precisely the field widths specified in the input format are read, with no attempt to align to record boundaries (i.e. after carriage returns).
For ACCESS = TRANSPARENT and FORM = UNFORMATTED, on output the values in the I/O list are output to file in their internal format, with no surrounding record structure (unlike sequential unformatted). Similarly, on input, the values in the input list are read in direct from file, without any record structure. This gives a Fortran binding for applications which would previously have called the I/O primitive subroutines OPENR@, OPENW@, READF@, WRITEF@ etc. directly.

ACTION=<string>
<string> specifies that READ, WRITE or READWRITE operations will be allowed on the file. The default is READWRITE.

BLANK=<string>
This option may only appear for files being connected for formatted input/output. <string> is a string which when the trailing spaces are removed is either NULL or ZERO. The default is NULL. If NULL, then all spaces in formatted numeric fields are ignored, if ZERO then spaces in formatted numeric fields are treated as zeros.

DELIM=<string>
<string> specifies the delimiter to be used in character constants. It is always one of APOSTROPHE , QUOTE or NONE. The default is NONE.

PAD=<string>
<string> specifies whether input records are space padded. The value is either YES or NO. The default is YES.

POSITION=<string>
<string> is a character expression that, when trailing spaces have been removed, is ASIS , REWIND or APPEND.

RECL=<value>
<value> is an integer expression, which gives record length in bytes. For direct access files, this specifier is obligatory.
As an extension to the standard, RECL may also be specified for a file opened for sequential access. This causes fixed length records to be read from or written to file and allows a BACKSPACE to be followed by a WRITE.

STATUS=<string>
<string> is a character expression which, when trailing spaces are removed, gives the value OLD, NEW, REPLACE, SCRATCH or UNKNOWN.

The following options for <string> are not in the ANSI Standard:

'APPEND'
A FILE= specifier must also be used. 'APPEND' is allowed for both formatted and unformatted files opened for sequential access. Output is appended to <filename> if it exists - if <filename> does not exist, it will be created.

'MODIFY'
A FILE= specifier must also be used - <filename> need not exist. If <filename> does not exist, 'MODIFY' is equivalent to 'NEW'. If <filename> exists, 'MODIFY' causes the existing file to be truncated and overwritten.

'READONLY'
A FILE= specifier must also be used and <filename> must exist. READONLY status ensures that any attempt to write a record to <filename> causes a run-time error. It also enables a file to be opened for reading more than once.

SHARE=<access mode>
The operating system provides a means whereby a program, when opening a file, can define the access that other programs are allowed to a file for the period that the first program has the file open.

This file sharing mechanism applies for multiple instances of the same file opened by a particular program, for two or more programs running on the same machine, or by two or more programs running on different machines (e.g. access via a shared disk on a network).

When a program opens a file, it can specify that it requires read access, write access, or read and write access. In addition to this, it can specify the <access mode> that other programs are permitted while it still has the file open.

<access mode> is a character expression whose value is one of the following:

'COMPAT'

Compatibility mode - equivalent to opening the file with no sharing attributes. No other program will be able to access the file while this program has it open.

'DENYRW'

Exclusive - no other program can access the file while it is open.

'DENYWR'

Other programs cannot access the file for write or read/write access, but can open the file for read only access.

'DENYRD'

Other programs cannot access the file for read or read/write access, but can open the file for write only access.

'DENYNONE'

Other programs can access the file for read, write or read/write access.

Note that a second or subsequent program attempting to open the file will be denied access in all cases if it attempts to open the file in compatibility mode. All attempts to open a file that may be in use by another program must use one of the other modes, and thereby must specify the access to be granted to other programs trying to access the file subsequently.

DRIVER=<string>
DRIVER is an extension to the ANSI Standard. <string> is the name of a subroutine which has been previously declared in an EXTERNAL statement. If DRIVER= appears, then FILE=, FILETYPE= and STATUS= must not appear. For further information see the FTN77 User's Guide available in PDF form on the Silverfrost website.

FILETYPE=<string>
FILETYPE is an extension to the ANSI Standard. If FILETYPE='TTY', input and output is read from the keyboard and written to the screen via the appropriate unit number.
Trailing spaces are removed when writing a string to a file.
Note that other devices such as LPT1 may be available by simply opening 'files' of the corresponding name. For example, to write directly to the printer from unit 6 it is often possible to execute: OPEN(UNIT=6,FILE='LPT1').

Reading tab characters
In a Fortran READ statement, by default, tab characters read from a file are converted to spaces. To avoid this conversion you should make a call to the subroutine READ_TABS@(unitno) immediately after the OPEN statement (unitno is the unit number of the stream).

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited