| Application Programmer's I/O Guide - S-3695-35 | ||
|---|---|---|
| Prev Section | Next Section | |
The Fortran standard describes program statements that you can use to transfer data between external media and internal files or between internal files and internal storage. It describes auxiliary I/O statements that can be used to change the position in the external file or to write an endfile record. It also describes auxiliary I/O statements that describe properties of the connection to a file or that inquire about the properties of that connection.
The Fortran standard specifies the form of the input data that a Fortran program processes and the form of output data resulting from a Fortran program. It does not specifically describe the physical properties of I/O records, files, and units. This section provides a general overview of files, records, and units.
Standard Fortran has two types of files: external and internal. An external file is any file that is associated with a unit number. An internal file is a character variable that is used as the unit specifier in a READ or WRITE statement. A unit is a means of referring to an external file. A unit is connected or linked to a file through the OPEN statement in standard Fortran. An external unit identifier refers to an external file and an internal file identifier refers to an internal file. See Section 2.2, for more information about unit identifiers.
A file can have a name that can be specified through the FILE= specifier in a Fortran OPEN statement. If no explicit OPEN statement exists to connect a file to a unit, and if assign(1) was not used, the I/O library uses a form of the unit number as the file name.
Internal files provide a means of transferring and converting text stored in character variables. An internal file must be a character variable or character array. If the file is a variable, the file can contain only one record. If the file is a character array, each element within the array is a record. On output, the record is filled with blanks if the number of characters written to a record is less than the length of the record. An internal file is always positioned at the beginning of the first record prior to data transfer. Internal files can contain only formatted records.
When reading and writing to an internal file, only sequential formatted data transfer statements that do not specify list-directed formatting may be used. Only sequential formatted READ and WRITE statements may specify an internal file.
In standard Fortran, one external unit may be connected to a file. Cray allows more than one external unit to be connected to the standard input, standard output, or standard error files if the files were assigned with the assign -D command. More than one external unit can be connected to a terminal.
External files have properties of form, access, and position as described in the following text. You can specify these properties explicitly by using an OPEN statement on the file. The Fortran standard provides specific default values for these properties.
Form (formatted or unformatted): external files can contain formatted or unformatted records. Formatted records are read or written by formatted I/O data transfer statements. Unformatted records are accessed through unformatted I/O data transfer statements. If the default does not match the form needed, you can specify the form by using an OPEN statement.
File access (sequential or direct access): external files can be accessed through sequential or direct access methods. The file access method is determined when the file is connected to a unit.
Sequential access does not require an explicit open of a file by using an OPEN statement.
When connected for sequential access, the external file has the following properties:
The records of the file are either all formatted or unformatted, except that the last record of the file may be an endfile record.
The records of the file must not be read or written by direct-access I/O statements when the file is opened for sequential access.
If the file is created with sequential access, the records are stored in the order in which they are written (that is, sequentially).
To use sequential access on a file that was created as a formatted direct-access file, open the file as sequential. To use sequential access on a file that was created as an unformatted direct-access file, open the file as sequential, and use the assign command on the file as follows:
assign -s unblocked ... |
The assign command is required to specify the type of file structure. The I/O libraries need this information to access the file correctly.
Buffer I/O files are unformatted sequential access files.
Direct access does require an explicit open of a file by using an OPEN statement. If a file is accessed through a sequential access READ or WRITE statement, the I/O library implicitly opens the file. During an explicit or implicit open of a file, the I/O library tries to access information generated by the assign(1) command for the file.
Direct access can be faster than sequential access when a program must access a set of records in a nonsequential manner.
When connected for direct access, an external file has the following properties:
The records of the file are either all formatted or all unformatted. If the file can be accessed as a sequential file, the endfile record is not considered part of the file when it is connected for direct access. Some sequential files do not contain a physical endfile record.
The records of the file must not be read or written by sequential-access I/O statements while the file is opened for direct access.
All records of the file have the same length, which is specified in the RECL specifier of the OPEN statement.
Records do not have to be read or written in the order of their record numbers.
The records of the file must not be read or written using list-directed or namelist formatting.
The record number (a positive integer) uniquely identifies each record.
If all of the records in the file are the same length and if the file is opened as direct access, a formatted sequential-access file can be accessed as a formatted direct-access file on UNICOS and UNICOS/mk systems.
Unformatted sequential-access files can be accessed as unformatted direct-access files if all of the records are the same length and if the file is opened as direct access, but only if the sequential-access file was created with an unblocked file structure. The following assign commands create these file structures:
assign -s unblocked ... assign -s u ... assign -F system ... |
For more information about the assign environment and about default file structures, see Chapter 6.
File position: a file connected to a unit has a position property, which can be either an initial point or a terminal point. The initial point of a file is the position just before the first record, and the terminal point is the position just after the last record. If a file is positioned within a record, that record is considered to be the current record; otherwise, there is no current record.
During an I/O data transfer statement, the file can be positioned within a record as each individual input/out or in/out list (iolist) item is processed. The use of a dollar sign ($) or a backslash (\) as a carriage control edit descriptor in a format may cause a file to be positioned within a record.
In standard Fortran, the end-of-file (EOF) record is a special record in a sequential access file; it denotes the last record of a file. A file can be positioned after an EOF, but only CLOSE, BACKSPACE, or REWIND statements are then allowed on the file in standard Fortran. Other I/O operations are allowed after an EOF to provide multiple-file I/O if a file is assigned to certain devices or is assigned with a certain file structure.
| Prev Section | Table of Contents | Title Page | Next Section |
| Introduction | Up one level | Fortran unit identifiers |