A direct-access file contains a number of records that are written to or read from by referring to the record number. Direct access is also called random access.
In direct access:
Records must be all the same length.
Records are usually all the same type.
A logical record in a direct access, external file is a string of bytes of a length specified when the file is opened.
Read and write statements must not specify logical records longer than the original record size definition.
Shorter logical records are allowed.
Unformatted direct writes leave the unfilled part of the record undefined.
Formatted direct writes pass the unfilled record with blanks.
In using direct unformatted I/O, be careful with the number of values your program expects to read.
Direct access READ and WRITE statements have an argument, REC=n, which gives the record number to be read or written. An alternate, nonstandard form is 'n.
OPEN( 2, FILE='data.db', ACCESS='DIRECT', RECL=20, & FORM='UNFORMATTED', ERR=90 ) READ( 2, REC=13, ERR=30 ) X, Y READ( 2 ' 13, ERR=30 ) X, Y ! Alternate form @
This code opens a file for direct-access, unformatted I/O, with a record length of 20 characters, then reads the thirteenth record as is.
OPEN( 2, FILE='inven.db', ACCESS='DIRECT', RECL=20, & FORM='FORMATTED', ERR=90 ) READ( 2, FMT='(I10,F10.3)', REC=13, ERR=30 ) A, B
This code opens a file for direct-access, formatted I/O, with a record length of 20 characters, then reads the thirteenth record and converts it according to the (I10,F10.3) format.