Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 10 HP Fortran 90 statements

OPEN

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

Connects file to a unit.

Syntax

OPEN (io-specifier-list)
io-specifier-list

is a list of the following comma-separated I/O specifiers:

[UNIT=]unit

specifies the unit to connect to an external file. unit must be an integer expression that evaluates to a number greater than 0. If the optional keyword UNIT= is omitted, unit must be the first item in io-specifier-list.

ACCESS=character-expression

specifies the method of file access. character-expression can be one of the following arguments:

'DIRECT'Open file for direct access.
"SEQUENTIAL"Open file for sequential access (default).
" POSITION= APPEND"To open a file for append (to position the file just before the end-of-file record)

ACTION=character-expression

specifies the allowed data-transfer operations. character-expression can be one of the following arguments:

'READ'Do not allow WRITE and ENDFILE statements.
'WRITE'Do not allow READ statements.
'READWRITE'Allow any data transfer statement (default).

BLANK=character-expression

specifies treatment of blanks within numeric data on input. This specifier is applicable to formatted input only. character-expression can be one of the following arguments:

'NULL'Ignore blanks (default).
'ZERO'Substitute zeroes for blanks.

DELIM=character-expression

specifies the delimiter to use (if any) when delimiting character constants in list-directed and namelist-directed formatting. This specifier is applicable to formatted output only. character-expression can be one of the following arguments:

'APOSTROPHE'Use the apostrophe to delimit character constants in list-directed and namelist-directed formatting.
'QUOTE'Use double-quotation marks to delimit character constants in list-directed and namelist-directed formatting.
'NONE'Use no delimiter to delimit character constants in list-directed and namelist-directed formatting (default).

ERR=stmt-label

specifies the label of the executable statement to which control passes if an error occurs during statement execution.

FILE=character-expression

specifies the name of the file to be connected to unit. character-expression can also be the ASCII representation of a device file. If this specifier does not appear in the OPEN statement, a temporary scratch file is created.

FORM=character-expression

specifies whether the file is connected for formatted or unformatted I/O. character-expression can be one of the following arguments:

'FORMATTED'Specify formatted I/O. If the file is to be opened for sequential access, this is the default.
'UNFORMATTED'Specify unformatted I/O. If the file is to be opened for direct access, this is the default.

IOSTAT=integer-variable

returns the I/O status after the statement executes. If the statement successfully executes, integer-variable is set to zero. If an error occurs, it is set to a positive integer that indicates which error occurred.

PAD=character-expression

specifies whether or not to pad the input record with blanks if the record contains fewer characters than required by the format specification. This specifier is applicable to formatted input only. character-expression can be one of the following arguments:

'YES'Pad input records with blanks (if necessary) to fill it out to length required by format specification (default).
'NO'Do not pad input record with blanks if it is not as long as record specified by format specification.

POSITION=character-expression

specifies the position of an existing file to be opened for sequential access. character-expression can be one of the following arguments:

'ASIS'Leave file position unchanged (default).
'REWIND'Position the file at its start.
'APPEND'Position the file just before the end-of-file record.

If the file to be opened does not exist, this specifier is ignored. New files are always positioned at their start.

RECL=integer-expression

specifies the length of each record in a file to be opened for direct access. The length is measured in characters (bytes). This specifier must be present when a file is opened for direct access and is ignored if file is opened for sequential access.

STATUS=character-expression

specifies the state of the file when it is opened. character-expression can be one of the following arguments:

'OLD'Open an existing file. FILE= must also be specified and the named file must exist.
'NEW'Create a new file. FILE= must also be specified and the named file must not exist.
'UNKNOWN'If the file named in FILE= exists, open it with the status of OLD; if it does not exist, open it with the status of NEW. This is the default status.
'REPLACE'If the file does not exist, create it with a status of OLD; if it does exist, delete it and open it with a status of NEW. If STATUS='REPLACE' is specified, FILE= must also be specified.
'SCRATCH'Create a scratch file. FILE= specifier must not be specified. For information about scratch files, see “Scratch files”.

Description

The OPEN statement connects a unit to a file so that data can be read from or written to that file. Once a file is connected to a unit, the unit can be referenced by any program unit in the program.

I/O specifiers do not have to appear in any specific order in the OPEN statement. However, if the optional keyword UNIT= is omitted, unit must be the first item in the list.

Only one unit can be connected to a file at a time. That is, the same file cannot be connected to two different units. Attempting to open a file that is connected to a different unit will produce undefined results.

However, multiple OPENs can be performed on the same unit. In other words, if a unit is connected to a file that exists, it is permissible to execute another OPEN statement for the same unit. If FILE= specifies a different file, the previously opened file is automatically closed before the second file is connected to the unit. If FILE= specifies the same file, the file remains connected in the same position; the values of the BLANK=, DELIM=, PAD=, ERR=, and IOSTAT= specifiers can be changed, but attempts to change the values of any of the other specifiers will be ignored.

Examples

The following examples illustrate different uses of the OPEN statement.

Opening a file for sequential access

The following OPEN statement connects the existing file inv to unit 10 and opens it (by default) for sequential access. Only READ statements are permitted to perform data transfers. If an error occurs, control passes to the executable statement labeled 100 and the error code is placed in the variable ios:

OPEN(10, FILE="inv", ERR=100, I0STAT=ios,  &
ACTION="READ", STATUS="OLD")

Opening a file for direct access

The following OPEN statement opens the file whose name is contained in the variable next1, connecting it to unit 4 as a formatted, direct-access file with a record length of 50 characters:

OPEN(ACCESS="DIRECT", UNIT=4, RECL=50,  &
FORM="FORMATTED", FILE=next1)

Opening a device for I/O transfers

The next example connects the system device /dev/console to unit 6; all data transfers that specify unit 6 will go to this device:

OPEN(6,FILE="/DEV/CONSOLE")

Opening a scratch file

The following two OPEN statements produce the same results: open a scratch file that is connected to unit 19 (if the FILE=namespecifier had appeared in the first statement, the named file would have been opened instead):

OPEN (UNIT=19)
OPEN (UNIT=19, STATUS="SCRATCH")

I/O specifiers in an OPEN statement

Because the I/O specifiers that can be used in an OPEN statement do not have to appear in any specific order, the following three OPEN statements are all equivalent:

OPEN(UNIT=3, STATUS="NEW", FILE="OUT.DAT")
OPEN(3, STATUS="NEW", FILE="OUT.DAT")
OPEN(STATUS="NEW", FILE="OUT.DAT", UNIT=3)

Note, however, that in the second OPEN statement the number 3 must appear first because of the omission of the optional keyword UNIT=. Thus, the following OPEN statement is illegal:

OPEN(STATUS="NEW", 3, FILE="OUT.DAT") ! illegal

Related statements

CLOSE, INQUIRE, READ, and WRITE

Related concepts

For information about I/O concepts and examples of programs that perform I/O, see Chapter 8 “I/O and file handling”. For information about I/O formatting, see Chapter 9 “I/O formatting”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.