[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index


Chapter 12
File Operation I/O Statements

This chapter contains information on the following file connection, inquiry, and positioning statements:

  • BACKSPACE ( Section 12.1)
    Positions a sequential file at the beginning of the preceding record.
  • CLOSE ( Section 12.2)
    Terminates the connection between a logical unit and a file or device.
  • DELETE ( Section 12.3)
    Deletes a record from a relative or indexed file.
  • ENDFILE ( Section 12.4)
    For sequential files, writes an end-of-file record to the file and positions the file after this record. For direct access files, truncates the file after the current record.
  • INQUIRE ( Section 12.5)
    Requests information on the status of specified properties of a file or logical unit.
  • OPEN ( Section 12.6)
    Connects a Fortran logical unit to a file or device; declares attributes for read and write operations.
  • REWIND ( Section 12.7)
    Positions a sequential file to the beginning of that file.
  • UNLOCK ( Section 12.8)
    Frees a record in a sequential, relative, or indexed file that was locked by a previous READ statement.

For More Information:

  • On data transfer I/O statements, see Chapter 10.
  • On control specifiers, see Section 10.2.1.
  • On record position, advancement, and transfer, see the HP Fortran for OpenVMS User Manual.

12.1 BACKSPACE Statement

The BACKSPACE statement positions a sequential file at the beginning of the preceding record, making it available for subsequent I/O processing. It takes one of the following forms:


  • BACKSPACE ([UNIT=]io-unit [,ERR=label] [,IOSTAT=i-var])
  • BACKSPACE io-unit

io-unit

Is an external unit specifier.

label

Is the label of the branch target statement that receives control if an error occurs.

i-var

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

Rules and Behavior

The I/O unit number must specify an open file on disk or magnetic tape.

A BACKSPACE statement must not be specified for a file that is open for direct, append, or keyed access, because record n is not available to the RMS I/O system.

If a file is already positioned at the beginning of a file, a BACKSPACE statement has no effect.

Examples

The following statement repositions the file connected to I/O unit 4 back to the preceding record:


BACKSPACE 4

Consider the following statement:


BACKSPACE (UNIT=9, IOSTAT=IOS, ERR=10)

This statement positions the file connected to unit 9 back to the preceding record. If an error occurs, control is transferred to the statement labeled 10, and a positive integer is stored in variable IOS.

For More Information:

12.2 CLOSE Statement

The CLOSE statement disconnects a file from a unit. It takes the following form:


  • CLOSE ([UNIT=]io-unit [,
    • STATUS
    • DISPOSE
    • DISP
    =p] [,ERR=label] [,IOSTAT=i-var])

io-unit

Is an external unit specifier.

p

Is a scalar default character expression indicating the status of the file after it is closed. It has one of the following values:
' KEEP ' or ' SAVE ' Retains the file after the unit closes.
' DELETE ' Deletes the file after the unit closes. 1
' PRINT ' 2 Submits the file to the line printer spooler, then retains it.
' PRINT/DELETE ' 2 Submits the file to the line printer spooler, then deletes it.
' SUBMIT ' Submits the file to the batch job queue, then retains it.
' SUBMIT/DELETE ' Submits the file to the batch job queue, then deletes it.

1Unless OPEN(READONLY) is in effect.
2Use only on sequential files.

The default is ' DELETE ' for scratch files. For all other files, the default is ' KEEP ' .

label

Is the label of the branch target statement that receives control if an error occurs.

i-var

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

Rules and Behavior

The CLOSE statement specifiers can appear in any order. An I/O unit must be specified, but the UNIT specifier is optional if the unit specifier is the first item in the I/O control list.

The status specified in the CLOSE statement supersedes the status specified in the OPEN statement, except that a file opened as a scratch file cannot be saved, printed, or submitted, and a file opened for read-only access cannot be deleted.

If a CLOSE statement is specified for a unit that is not open, it has no effect.

Examples

Consider the following statement:


CLOSE (UNIT=J, STATUS='DELETE', ERR=99)

This statement closes the file connected to unit J and deletes it. If an error occurs, control is transferred to the statement labeled 99.

Consider the following statement:


CLOSE (UNIT=1, STATUS='PRINT')

This statement closes the file on unit 1 and submits it for printing.

For More Information:

12.3 DELETE Statement

The DELETE statement deletes a record from a relative or indexed organization file. It takes one of the following forms:

Keyed Access


  • DELETE ([UNIT=]io-unit [,ERR=label] [,IOSTAT=i-var])

Direct Access


  • DELETE ([UNIT=]io-unit [,REC=r] [,ERR=label] [,IOSTAT=i-var])

io-unit

Is an external unit specifier.

r

Is a scalar numeric expression indicating the record number to be deleted.

label

Is the label of the branch target statement that receives control if an error occurs.

i-var

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

Rules and Behavior

In files with keyed access, the DELETE statement deletes the current record. The current record is the last record that is accessed by a READ statement on the specified external unit.

In files with direct access, the DELETE statement deletes the direct access record specified by r. If REC=r is omitted, the current record is deleted. When the direct access record is deleted, any associated variable is set to the next record number.

The DELETE statement logically removes the appropriate record from the specified file by locating the record and marking it as a deleted record. It then frees the position formerly occupied by the deleted record so that a new record can be written into that position.

Examples

The following statement deletes the fifth record in the file connected to I/O unit 10:


DELETE (10, REC=5)

In the next example, the current record is deleted from the file connected to I/O unit 11:


DELETE (11)

For More Information:

12.4 ENDFILE Statement

For sequential files, the ENDFILE statement writes an end-of-file record to the file and positions the file after this record (the terminal point). For direct access files, the ENDFILE statement truncates the file after the current record.

An ENDFILE statement takes one of the following forms:


  • ENDFILE ([UNIT=]io-unit [,ERR=label] [,IOSTAT=i-var])
  • ENDFILE io-unit

io-unit

Is an external unit specifier.

label

Is the label of the branch target statement that receives control if an error occurs.

i-var

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

Rules and Behavior

If the unit specified in the ENDFILE statement is not open, the default file is opened for unformatted output.

An end-of-file record can be written only to files with sequential organization that are accessed as formatted-sequential or unformatted-segmented sequential files.

An ENDFILE statement performed on a direct access file always truncates the file.

An ENDFILE statement must not be issued for a file that is open for keyed access.

An end-of-file record written to a file on magnetic tape is not the same as a tape mark.

End-of-file records should not be written in files that are read by programs written in a language other than Fortran, because other languages do not support the embedded end-of-file concept.

Examples

The following statement writes an end-of-file record to I/O unit 2:


ENDFILE 2

Suppose the following statement is specified:


ENDFILE (UNIT=9, IOSTAT=IOS, ERR=10)

An end-of-file record is written to the file connected to unit 9. If an error occurs, control is transferred to the statement labeled 10, and a positive integer is stored in variable IOS.

For More Information:

  • On the UNIT control specifier, see Section 10.2.1.1.
  • On the ERR control specifier, see Section 10.2.1.8.
  • On the IOSTAT control specifier, see Section 10.2.1.7.
  • On record position, advancement, and transfer, see the HP Fortran for OpenVMS User Manual.

12.5 INQUIRE Statement

The INQUIRE statement returns information on the status of specified properties of a file or logical unit. It takes one of the following forms:

Inquiring by File


  • INQUIRE (FILE=name [,ERR=label] [,IOSTAT=i-var] [,DEFAULTFILE=def], slist)

Inquiring by Unit


  • INQUIRE ([UNIT=]io-unit [,ERR=label] [,IOSTAT=i-var], slist)

Inquiring by Output List


  • INQUIRE (IOLENGTH=len) out-item-list

name

Is a scalar default character expression specifying the name of the file for inquiry.

label

Is the label of the branch target statement that receives control if an error occurs.

i-var

Is a scalar integer variable that is defined as a positive integer if an error occurs and zero if no error occurs.

def

Is a scalar default character expression specifying a default file name specification string. (For more information on the DEFAULTFILE specifier, see the Section 12.6.10.)

slist

Is one or more inquiry specifiers. Each specifier can appear only once. (The inquiry specifiers are described individually in the following sections.)

io-unit

Is an external unit specifier.

The unit does not have to exist, nor does it need to be connected to a file. If the unit is connected to a file, the inquiry encompasses both the connection and the file.

len

Is a scalar integer variable that is assigned a value corresponding to the length of an unformatted, direct-access record resulting from the use of the out-item-list in a WRITE statement.

The value is suitable to use as a RECL specifier value in an OPEN statement that connects a file for unformatted, direct access.

The unit of the value is 4-byte longwords, by default. However, if you specify the compiler option /ASSUME=BYTERECL, the unit is bytes.

out-item-list

Is a list of one or more output items (see Section 10.2.2).

Rules and Behavior

The control specifiers ([UNIT=]io-unit, ERR=label, and IOSTAT=i-var) and inquiry specifiers can appear anywhere within the parentheses following INQUIRE. However, if the UNIT specifier is omitted, the io-unit must appear first in the list.

An INQUIRE statement can be executed before, during, or after a file is connected to a unit. The specifier values returned are those that are current when the INQUIRE statement executes.

To get file characteristics, specify the INQUIRE statement after opening the file.

Examples

The following are examples of INQUIRE statements:


INQUIRE (FILE='FILE_B', EXIST=EXT)
INQUIRE (4, FORM=FM, IOSTAT=IOS, ERR=20)
INQUIRE (IOLENGTH=LEN) A, B

In the last statement, you can use the length returned in LEN as the value for the RECL specifier in an OPEN statement that connects a file for unformatted direct access. If you have already specified a value for RECL, you can check LEN to verify that A and B are less than or equal to the record length you specified.

For More Information:

12.5.1 ACCESS Specifier

The ACCESS specifier asks how a file is connected. It takes the following form:


  • ACCESS = acc

acc

Is a scalar default character variable that is assigned one of the following values:
' SEQUENTIAL ' If the file is connected for sequential access
' DIRECT ' If the file is connected for direct access
' KEYED ' If the file is connected for keyed access
' UNDEFINED ' If the file is not connected

12.5.2 ACTION Specifier

The ACTION specifier asks which I/O operations are allowed for a file. It takes the following form:


  • ACTION = act

act

Is a scalar default character variable that is assigned one of the following values:
' READ ' If the file is connected for input only
' WRITE ' If the file is connected for output only
' READWRITE ' If the file is connected for both input and output
' UNDEFINED ' If the file is not connected

12.5.3 BLANK Specifier

The BLANK specifier asks what type of blank control is in effect for a file. It takes the following form:


  • BLANK = blnk

blnk

Is a scalar default character variable that is assigned one of the following values:
' NULL ' If null blank control is in effect for the file
' ZERO ' If zero blank control is in effect for the file
' UNDEFINED ' If the file is not connected, or it is not connected for formatted data transfer

12.5.4 BLOCKSIZE Specifier

The BLOCKSIZE specifier asks about the I/O buffer size. It takes the following form:


  • BLOCKSIZE = bks

bks

Is a scalar integer variable.

The bks is assigned the current size of the I/O buffer. If the unit or file is not connected, the value assigned is zero.


Previous Next Contents Index