Previous Table of Contents Next


1.2.20.1. Records

There are two kinds of records—data records and endfile records. A data record is a sequence of values. The values in a data record may be represented in one of two ways: formatted or unformatted. If the values are characters readable by a person, each character is one value and the data is formatted. For example, the statement

   write (unit=*, fmt=”(i1, a, i2)”) 6, “,”, 11

produces a record containing the following four character values: 6,11.

Unformatted data consists of values usually represented just as they are stored in computer memory.

Formatted Records

A formatted record is one that contains only formatted data. A formatted record may be created by a person typing at a terminal or by a Fortran program that converts values stored internally into character strings that form readable representations of those values. When formatted data is read into the computer, the characters must be converted to the computer’s internal representation of values, which is often a binary representation. Even character values may be converted from one character representation in the record to another internal representation. The length of a formatted record is the number of characters in it; the length may be zero.

Unformatted Records

An unformatted record is one that contains only unformatted data. Unformatted records usually are created by running a Fortran program, although, with the knowledge of how to form the bit patterns correctly, they could be created by other means. Unformatted data often requires less space on an external device. Also, it is usually faster to read and write unformatted data because no conversion is required. However, it is not as suitable for reading by humans, and usually it is not suitable for transferring data from one computer to another because the internal representation of values is machine dependent. The length of an unformatted data record depends on the number of values in it but is measured in some processor-dependent units such as machine words; the length may be zero. The length of an unformatted record that will be produced by a particular output list may be determined by the inquire statement.

Endfile Records

The other kind of record is the endfile record, which, at least conceptually, has no values and no length. There can be at most one endfile record in a file and it must be the last record. It is used to mark the end of a file.

1.2.20.2. Files

A file is a collection of records. The records of a file must be either all formatted or all unformatted, except that the file may contain an endfile record as the last record. A file may have a name, but the length of the names and the characters that may be used in the names depends on the system being used. The set of names that are allowed often is determined by the operating system as well as the Fortran compiler.

A distinction is made between files that are located on an external device, such as a disk, and files in memory accessible to the program. The two kinds of files are external files and internal files.

An external file usually is stored on a peripheral device, such as a tape, a disk, or a computer terminal. For each external file, there is a set of allowed access methods, a set of allowed forms (formatted or unformatted), a set of allowed actions, and a set of allowed record lengths. How these characteristics are established depends on the computer system you are using, but usually they are determined by a combination of requests by the user of the file and actions by the operating system.

Internal files are stored in memory as values of character variables. The character values may be created using all the usual means of assigning character values, or they may be created with an output statement using the variable as an internal file. If the variable is a scalar, the file has just one record; if the variable is an array, the file has one record for each element of the array. The length of the record is the number of characters declared or assumed for the character variable. Only formatted sequential access is permitted on internal files.

For example, if char_array is an array of two character strings declared by

   character (len = 7), dimension (2) :: char_array

the statement

   write (unit = char_array, fmt = “(f7.5, /, f7.5)”) 10/3.0, 10/6.0

produces the same effect as the assignment statements

   char_array (1) = “3.33333”
   char_array (2) = “1.66667”

An internal file was used to construct the format to print big integers in section 1.2.18.1.

1.2.20.3. File Access Methods

There are two access methods for external files: sequential access and direct access. Sequential access to the records in the file begins with the first record of the file and proceeds sequentially to the second record, and then to the next record, record by record. The records are accessed in the order that they appear in the file. It is not possible to begin at some particular record within the file without reading from the current record down to that record in sequential order.

When a file is accessed sequentially, the records are read and written sequentially. For example, if the records are written in any arbitrary order using direct access and then read using sequential access, the records are read beginning with the first record of the file, regardless of when it was written.

When a file is accessed directly, the records are selected by record number. Using this identification, the records may be read or written in any order. For example, it is possible to write record number 47 first and then write record number 13.

Each file has a set of permissible access methods, which usually means that it may be accessed either sequentially or directly.


Previous Table of Contents Next