Compaq Fortran
User Manual for
Tru64 UNIX and Linux Alpha Systems


Previous Contents Index


Chapter 7
Compaq Fortran Input/Output (I/O)

This chapter describes input/output (I/O) as implemented for Compaq Fortran and discusses the following topics:

For More Information

7.1 Logical I/O Units

In Compaq Fortran, a logical unit is a channel through which data transfer occurs between the program and a device or file. You identify each logical unit with a logical unit number, which can be any nonnegative integer from 0 to a maximum value of 2,147,483,647 (2**31--1). For example:


READ (2,100) I,X,Y 

This READ statement specifies that data is to be entered from the device or file corresponding to logical unit 2, in the format specified by the FORMAT statement labeled 100. When opening a file, use the UNIT specifier to indicate the unit number.

Fortran 95/90 (Fortran) programs are inherently device-independent. The association between the logical unit number and the physical file can occur at run-time. Instead of changing the logical unit numbers specified in the source program, you can change this association at run time to match the needs of the program and the available resources. For example, before running the program, a script file can set the appropriate environment variable or allow the terminal user to type a directory path, file name, or both.

Use the same logical unit number specified in the OPEN statement for other I/O statements to be applied to the opened file, such as READ and WRITE.

The OPEN statement connects a unit number with an external file and allows you to explicitly specify file attributes and run-time options using OPEN statement specifiers (all files except internal files are called external files).

Certain unit numbers are preconnected to standard devices. Unit number 5 is associated with stdin , unit 6 with stdout , and unit 0 with stderr . At run time, if units 5 and 6 are specified by a record I/O statement (such as READ or WRITE) without having been explicitly opened by an OPEN statement, Compaq Fortran implicitly opens units 5, 6, and 0 and associates them with their respective operating system standard I/O files (if the corresponding FORTn environment variable is not set).

For More Information

On the OPEN statement and preconnected files, see Section 7.5.1.

7.2 Types of I/O Statements

Table 7-1 lists the Compaq Fortran I/O statements.

Table 7-1 Summary of I/O Statements
Category and Statement Name Description
File Connection  
OPEN Connects a unit number with an external file and specifies file connection characteristics.
CLOSE Disconnects a unit number from an external file.
File Inquiry  
INQUIRE Returns information about a named file, a connection to a unit, or the length of an output item list.
Record Position  
BACKSPACE Moves the record position to the beginning of the previous record (sequential access only).
ENDFILE Writes an end-of-file marker after the current record (sequential access only).
REWIND Sets the record position to the beginning of the file (sequential access only).
Record Input  
READ Transfers data from an external file record or an internal file to internal storage.
Record Output  
WRITE Transfers data from internal storage to an external file record or to an internal file.
PRINT Transfers data from internal storage to stdout (standard output device). Unlike WRITE, PRINT only provides formatted sequential output and does not specify a unit number.
Compaq Fortran Extensions  
ACCEPT Reads input from stdin . Unlike READ, ACCEPT only provides formatted sequential output and does not specify a unit number.
DELETE Marks a record at the current record position in a relative file as deleted (direct access only).
REWRITE Transfers data from internal storage to an external file record at the current record position (direct access relative files only).
UNLOCK Releases a lock held on the current record when file sharing was requested when the file was opened. Ignored in the current version of Compaq Fortran for Tru64 UNIX and Linux Systems (see Section 7.6.2).
TYPE Writes record output to stdout (same as PRINT).
DEFINE FILE Specifies file characteristics for a direct access relative file and connects the unit number to the file, similar to an OPEN statement. Provided for compatibility with compilers before FORTRAN-77.
FIND Changes the record position in a direct access file. Provided for compatibility with compilers older than FORTRAN-77.

In addition to the READ, WRITE, REWRITE, TYPE, and PRINT statements, other I/O record-related statements are limited to a specific file organization. For instance:

The file-related statements (OPEN, INQUIRE, and CLOSE) apply to any relative or sequential file.

Note

1 Detecting deleted records is only available if the -vms option was specified when the program was compiled. For more information on the -vms option, see Section 3.87.

7.3 Forms of I/O Statements

Each type of record I/O statement can be coded in a variety of forms. The form you select depends on the nature of your data and how you want it treated. When opening a file, specify the form using the FORM specifier. The following are the forms of I/O statements:

Formatted, list-directed, and namelist I/O forms require translation of data from internal (binary) form within a program to external (readable character) form in the records. Consider using unformatted I/O for the following reasons:

To write data to a file using formatted, list-directed, or namelist I/O statements, specify FORM= ' FORMATTED ' when opening the file. To write data to a file using unformatted I/O statements, specify FORM= ' UNFORMATTED ' when opening the file.

Data written using formatted, list-directed, or namelist I/O statements is referred to as formatted data; data written using unformatted I/O statements is referred to as unformatted data.

When reading data from a file, you should use the same I/O statement form that was used to write the data to the file. For instance, if data was written to a file with a formatted I/O statement, you should read data from that file with a formatted I/O statement.

Although I/O statement form is usually the same for reading and writing data in a file, a program can read a file containing unformatted data (using unformatted input) and write it to a separate file containing formatted data (using formatted output). Similarly, a program can read a file containing formatted data and write it to a different file containing unformatted data.

As described in Section 7.6.2, you can access records in any sequential or relative file using sequential access. For relative files and certain (fixed-length) sequential files, you can also access records using direct access.

Table 7-2 shows the main record I/O statements, by category, that can be used in Compaq Fortran programs.

Table 7-2 Available I/O Statements and Record I/O Forms
File Type, Access, and I/O Form Available Statements
External file, sequential access
Formatted
List-Directed
Namelist
Unformatted
READ, WRITE, PRINT, ACCEPT 1, TYPE 1, REWRITE 1,2
READ, WRITE, PRINT, ACCEPT 1, TYPE 1, REWRITE 1,2
READ, WRITE, PRINT, ACCEPT 1, TYPE 1, REWRITE 1,2
READ, WRITE, REWRITE 1,2
External file, direct access
Formatted
Unformatted
READ, WRITE, and REWRITE 1,2
READ, WRITE, and REWRITE 1,2
Internal file 3
Formatted
List-Directed
Unformatted
READ, WRITE
READ, WRITE
None


1This statement is a Compaq extension to the Fortran 95/90 standard.
2You can use the REWRITE statement only for relative files, using direct access.
3An internal file is a way to reference character data in a buffer using sequential access (see Section 7.4.2).

7.4 Types of Files and File Characteristics

This section discusses file organization, internal and scratch files, record type, record length, and other file characteristics.

7.4.1 File Organizations

File organization refers to the way records are physically arranged on a storage device.

The default file organization is always ORGANIZATION= ' SEQUENTIAL ' for an OPEN statement.

Compaq Fortran supports two kinds of file organizations: sequential and relative. The organization of a file is specified by means of the ORGANIZATION specifier in the OPEN statement, as described in the Compaq Fortran Language Reference Manual.

You must store relative files on a disk device. You can store sequential files on magnetic tape or disk devices, and can use other peripheral devices, such as terminals, pipes, and line printers as sequential files.

Sequential Organization

A sequentially organized file consists of records arranged in the sequence in which they are written to the file (the first record written is the first record in the file, the second record written is the second record in the file, and so on). As a result, records can be added only at the end of the file.

Sequential files are usually read sequentially, starting with the first record in the file. Sequential files with a fixed-length record type that are stored on disk can also be accessed by relative record number (direct access).

Relative Organization

Within a relative file are numbered positions, called cells. These cells are of fixed equal length and are consecutively numbered from 1 to n, where 1 is the first cell, and n is the last available cell in the file. Each cell either contains a single record or is empty.

Records in a relative file are accessed according to cell number. A cell number is a record's relative record number (its location relative to the beginning of the file). By specifying relative record numbers, you can directly retrieve, add, or delete records regardless of their locations (direct access). (Detecting deleted records is only available if you specified the -vms option when the program was compiled. For information on the -vms option, see Section 3.87.)

When creating a relative file, specify the RECL value to determine the size of the fixed-length cells. Within the cells, you can store records of varying length, as long as their size does not exceed the cell size.

7.4.2 Internal Files and Scratch Files

Compaq Fortran also supports two other types of files that are not file organizations---namely, internal files and scratch files.

Internal Files

When you use sequential access, you can use an internal file to reference character data in a buffer. The transfer occurs between internal storage and internal storage (unlike external files), such as between character variables and a character array.

An internal file consists of any of the following:

Instead of specifying a unit number for the READ or WRITE statement, use an internal file specifier in the form of a character scalar memory reference or a character-array name reference.

An internal file is a designated internal storage space (variable buffer) of characters that is treated as a sequential file of fixed-length records. To perform internal I/O, use formatted and list-directed sequential READ and WRITE statements. You cannot use file-related statements such as OPEN and INQUIRE on an internal file (no unit number is used).

If an internal file is made up of a single character variable, array element, or substring, that file comprises a single record whose length is the same as the length of the character variable, array element, or substring it contains. If an internal file is made up of a character array, that file comprises a sequence of records, with each record consisting of a single array element. The sequence of records in an internal file is determined by the order of subscript progression.

A record in an internal file can be read only if the character variable, array element, or substring comprising the record has been defined (a value has been assigned to the record).

Prior to each READ and WRITE statement, an internal file is always positioned at the beginning of the first record.

Scratch Files

Scratch files are created by specifying STATUS= ' SCRATCH ' in an OPEN statement. By default, these temporary files are created in (and later deleted from) the directory specified in the OPEN statement DEFAULTFILE (if specified). To request a different directory to contain the scratch (temporary) files, set the TMPDIR environment variable.

7.4.3 Record Types, Record Overhead, and Maximum Record Length

Record type refers to whether records in a file are all the same length, are of varying length, or use other conventions to define where one record ends and another begins.

You can use fixed-length and variable-length record types with sequential or relative files. You can use any of the record types with sequential files. Relative files require the fixed-length record type.

Records are stored in one the following record types:

Before you choose a record type, consider whether your application will use formatted or unformatted data. If you are using formatted data, you can choose any record type except segmented. Avoid the stream, stream_CR, and stream_LF record types when using unformatted data.

The segmented record type can only be used for unformatted sequential access with sequential files. You should not use segmented records for files that are read by programs written in languages other than Compaq Fortran products.

The stream, stream_CR, stream_LF, and segmented record types can be used only with sequential files.

7.4.3.1 Portability Considerations of Record Types

Consider the following portability needs when choosing a record type:

For more information on porting Compaq OpenVMS Fortran data, see Section A.4.

The default record type (RECORDTYPE) depends on the values for the ACCESS and FORM specifiers for the OPEN statement, as described in the Compaq Fortran Language Reference Manual.

When creating a new file or opening an existing file, specify one of the record types listed in Table 7-3.

Table 7-3 Compaq Fortran Record Types
Record Type Description
Fixed-length Records in a file must contain the same length. You must specify the record length (RECL) when the file is opened and can obtain it before opening the file with unformatted data using a form of the INQUIRE statement (see Section 7.5.2.3). For detailed information on fixed-length record format, see Section 7.8.1.
Variable-length Records in a file can vary in length. This is the most portable record type across multivendor U*X platforms (such as Compaq Tru64 UNIX). Record length information is stored as control bytes at the beginning and end of each record. For detailed information on variable-length record format (including control bytes), see Section 7.8.2.
Segmented A single logical record containing one or more unformatted records of varying length, which can only be used for unformatted sequential access.

The segmented record type is unique to Compaq Fortran products. Avoid the segmented record type when the application requires that the same file be used for programs written in languages other than Fortran and for non-Compaq platforms. However, because the segmented record type is unique to Compaq Fortran products, segmented files can be ported across Compaq Fortran platforms.

For detailed information on segmented record format, see Section 7.8.3.

Stream A stream file is not grouped into records and uses no record delimiters. Stream files contain character or binary data that is read or written to the extent of the variables specified. Specify CARRIAGECONTROL= ' NONE ' for stream files. For detailed information on stream file format, see Section 7.8.4.
Stream_LF and Stream_CR Records of varying length where the line feed (LF) or the carriage return (CR) character serve as record delimiters (LF for Stream_LF files and CR for Stream_CR files).

Stream_LF files must not contain embedded LF characters or use CARRIAGECONTROL= ' LIST ' . Instead, specify CARRIAGECONTROL= ' NONE ' . Stream_CR files must not contain embedded CR characters. The Stream_LF record type is the usual record type for text files.

For detailed information on stream_CR and stream_LF record formats, see Section 7.8.5.

The record type of the file is not maintained as an attribute of the file. The results of using a record type other than the one used to create the file are indeterminate.

For information on choosing the most efficient record type, see Section 5.5.9.

An I/O record is a collection of fields (data items) that are logically related and are usually processed as a unit.

Unless you specify nonadvancing I/O (ADVANCE specifier), each Compaq Fortran I/O statement transfers one record. The exceptions are described in Section 7.6.5.

7.4.3.2 Record Overhead

Record overhead refers to bytes associated with each record that are used internally by the file system and are not available when a record is read or written. Knowing the record overhead helps when estimating the storage requirements for an application. Although the overhead bytes exist on the storage media, do not include them when specifying the record length with the RECL specifier in an OPEN statement.

The various record types each require a different number of bytes for record overhead, as described in the following table:
Record Type Organization Record Overhead
Fixed-length Sequential files None.
Fixed-length Relative files None if the -vms option was omitted (the default). One byte if the -vms option was specified.
Variable-length Sequential files Eight bytes per record for overhead.
Segmented Sequential files Four bytes per record for overhead. One additional padding byte (space) is added if the specified record size is an odd number.
Stream Sequential files None required.
Stream_CR Sequential files One byte per record for overhead.
Stream_LF Sequential files One byte per record for overhead.


Previous Next Contents Index