# Record Access

Record access refers to how records will be read from or written to a file, regardless of the file's organization. Record access is specified each time you open a file; it can be different each time. The type of record access permitted is determined by the combination of file organization and record type.

For example, you can fo the following:

- Add records to a sequential file with ORGANIZATION= 'SEQUENTIAL' and POSITION= 'APPEND' (or use ACCESS= 'APPEND').

- Add records sequentially by using multiple WRITE statements, close the file, and then open it again with ORGANIZATION= 'SEQUENTIAL' and ACCESS= 'SEQUENTIAL' (or ACCESS= 'DIRECT' if the sequential file has fixed-length records).

## Sequential Access

Sequential access transfers records sequentially to or from files or I/O devices such as terminals. You can use sequential I/O with any type of supported file organization and record type.

When you select sequential access mode for files with sequential or relative organization, records are written to or read from the file starting at the beginning of the file and continuing through it, one record after another. A particular record can be retrieved only after all of the records preceding it have been read; new records can be written only at the end of the file.

## Direct Access

Direct access transfers records selected by record number to and from either sequential files stored on disk with a fixed-length record type or relative organization files.

If you select direct access mode, you can determine the order in which records are read or written. Each READ or WRITE statement must include the relative record number, indicating the record to be read or written.

You can directly access a sequential disk file only if it contains fixed-length records. Because direct access uses cell numbers to find records, you can enter successive READ or WRITE statements requesting records that either precede or follow previously requested records. For example, the first statement reads record 24; the second statement reads record 10:

    READ (12,REC=24) I
    READ (12,REC=10) J

## Stream Access

Stream access transfers bytes from records sequentially until a record terminator is found or a specified number of bytes have been read or written. Formatted stream records are terminated with a new line character; unformatted stream data contains no record terminators. Bytes can be read from or written to a file by byte position, where the first byte of the file is position 1. For example:

    OPEN (UNIT=12, ACCESS=’STREAM’)
    READ (12) I, J, K           ! start at the first byte of the file
    READ (12, POS=200) X, Y     ! then read staring at byte 200
    READ (12) A, B              ! then read starting where the previous READ stopped

The POS= specifier on INQUIRE can be used to determine the current byte position in the file.

### Note

The RECORDTYPE= specifier is ignored for stream access.

## Limitations of Record Access by File Organization and Record Type

You can use sequential and direct access modes on sequential and relative files. However, direct access to a sequential organization file can only be done if the file resides on disk and contains fixed-length records.

The table below summarizes the types of access permitted for the various combinations of file organizations and record types.

Record Type | Sequential Access? | Direct Access?
--- | --- | ---
Sequential file organization
Fixed | Yes | Yes
Variable | Yes | No
Segmented | Yes | No
Stream | Yes | No
Stream_CR | Yes | No
Stream_LF | Yes | No
Stream_CRLF | Yes | No
Relative file organization
Fixed | Yes | Yes

### Note

Direct access and relative files require that the file resides on a disk device.

Parent topic: Fortran I/O