Methods of File Access - Fortran

The following methods of file access are supported:

  • sequential
  • direct
  • keyed

External files can be accessed using any of the above methods. The access method is determined when the file is opened or defined. Fortran 77 requires that internal files must be accessed sequentially.

As an extension, the use of internal files in both formatted and unformatted I/O operations is permitted.

Sequential Access
A file connected for sequential access has the following properties:

  • For files that allow only sequential access, the order of the records is simply the order in which they were written.
  • For files that also allow direct access, the order of files depends on the record number. If a file is written sequentially, the first record written is record number 1 for direct access, the second written is record number 2, and so on.
  • Formatted and unformatted records cannot be mixed within a file.
  • The last record of the file can be an endfile record.
  • The records of a pure sequential file must not be read or written by direct-access I/O statements.

Direct Access

A file connected for direct access has the following properties:

  • A unique record number is associated with each record in a direct-access file. Record numbers are positive integers that are attached when the record is written. Records are ordered by their record numbers.
  • Formatted and unformatted records cannot be mixed in a file.
  • The file must not contain an endfile record if it is direct access only. If the file also allows sequential access, an endfile record is permitted but will be ignored while the file is connected for direct access.
  • All records of the file have the same length. When the record length of a direct-formatted file is one byte, the system treats the files as ordinary system files, that is, as byte strings in which each byte is addressable. A READ or WRITE request on such files consumes/produces bytes until satisfied, rather than restricting itself to a single record. Note that to produce a record length of one byte, the program must be compiled with the –old_rl option.
  • Only direct-access I/O statements can be used for reading and writing records. An exception is made when sequential I/O statements are used on a direct-unformatted file, in which case the next record is assumed. List-directed formatting is not permitted on direct-access files.
  • The record number cannot be changed once it is specified. A record can be rewritten, but it cannot be deleted.
  • Records can be read or written in any order.

Keyed Access

A file connected for keyed access has the following properties:

  • Only files having an indexed organization can be processed using the keyed-access method.
  • A unique character or integer value called a key is associated with one or more fields in each record of the indexed access file. The fields are defined when the file is created with an OPEN statement. Each READ statement contains a key to locate the desired record in the indexed file.
  • You can intermix keyed access and sequential access on the same opened file.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Fortran Topics