Direct-access files

A direct-access file is an array of records: every record has the same length, a record is named by its number, and a program can read or write record 900 without touching the first 899. It is the oldest random-access form of the language and it is still the simplest way to keep a table of fixed-size rows.

Fixed-length records

Two keywords set the form up: access="direct" says records are addressed by number, and recl=8 gives the length of every record in bytes for an unformatted file. A record of an integer and a real is eight bytes, which is why the example uses that number.

The file direct.f90:

program direct_demo implicit none integer :: u, i, id real :: v open (newunit=u, file="recs.dat", status="replace", access="direct", recl=8, action="write") do i = 1, 4 write (u, rec=i) i, 10.0 * i end do close (u) open (newunit=u, file="recs.dat", status="old", access="direct", recl=8, action="readwrite") read (u, rec=3) id, v print *, "record 3 :", id, v write (u, rec=2) 99, -1.0 read (u, rec=2) id, v print *, "record 2 :", id, v read (u, rec=9, iostat=i) id, v print *, "record 9 :", i close (u) end program direct_demo

gfortran -Wall -Wextra -std=f2018 direct.f90 -o direct
./direct

record 3 : 3 30.0000000
record 2 : 99 -1.00000000
record 9 : 5002

The file is thirty-two bytes, four records of eight, and it has no markers and no line endings: the record numbers are the layout. Record three was read without reading one and two, record two was replaced in place, and the read of record nine reported a positive status because the file has only four.

Writing and replacing a record

write (u, rec=n) replaces record n if it exists and creates it if it is the record after the last one. That is how a table is edited: read a record, change a value, write the record back to the same number, and nothing moves.

A record number beyond the last by more than one is an error, because a direct file has no gaps: the runtime cannot leave a hole it does not know how to fill. A program that grows a direct file appends records in order or writes a filled record where it wants one.

Formatted direct access

The same form works with text records, and then recl counts characters instead of bytes. Each record is exactly that many characters and is padded with blanks, and there is no line ending: the record number times the length is the position of the record in the file.

program direct_text_demo implicit none integer :: u character(len=10) :: s open (newunit=u, file="table.txt", status="replace", access="direct", recl=10, form="formatted", action="write") write (u, '(a)', rec=1) "alpha" write (u, '(a)', rec=2) "beta" write (u, '(a)', rec=3) "gamma" close (u) open (newunit=u, file="table.txt", status="old", access="direct", recl=10, form="formatted", action="read") read (u, '(a)', rec=3) s print *, "record 3 : [", s, "]" read (u, '(a)', rec=1) s print *, "record 1 : [", s, "]" close (u) end program direct_text_demo

gfortran -Wall -Wextra -std=f2018 direct_text.f90 -o direct_text
./direct_text

record 3 : [gamma ]
record 1 : [alpha ]

The file is thirty bytes and holds no newline at all: the five letters of alpha are followed by five blanks, then the same for the other two. A text tool shows the three records as one long line, which is the visible difference between this form and the text file of the earlier lesson.

Reading past the end

A read of a record that does not exist is not a crash when the read has an iostat: the status is positive and the record is untouched. That is how a program finds out how many records a direct file holds when it did not write the file itself: read until the status turns positive.

A loop that does that is the same as the record loop of the text lesson, with a record number the program counts instead of a pointer the runtime moves. The count is the last good number, which is the number of records.

What RECL means

For an unformatted direct file recl is the record length in bytes, and the values of one record must fit in it exactly: eight bytes for an integer and a real, sixteen for two reals of double precision. For a formatted direct file it is the number of characters of the record, and a value that prints wider than the record fills the record with asterisks or is truncated, depending on the descriptor.

The size of a direct file is the number of records times the record length, so a program can compute it, and the file has no other structure. That predictability is what direct access is for: a record is at a known byte, and a program that knows the number can go straight to it.

Exercises

  1. Add a fifth record to direct.f90 and inquire the file size. What is it, and why?
  2. Write record seven first, with records one to six missing. What does the open or the write report?
  3. Print the three records of table.txt with a loop over rec=1,3. Which characters does each record hold?
  4. Change recl of the text file to eight and write "gamma" again. What does the record hold now?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does access="direct" mean?

    Answer

    Records have a fixed length and are addressed by number

    Direct access names a record with REC= and fixes its length with RECL=.

  2. What is recl for an unformatted direct file?

    Answer

    The length of every record in bytes

    RECL is the record length, in bytes for unformatted files.

  3. What does this program print? program p implicit none integer :: u, v open (newunit=u, file="r.dat", status="replace", access="direct", recl=4, action="write") write (u, rec=1) 11 write (u, rec=2) 22 close (u) open (newunit=u, file="r.dat", status="old", access="direct", recl=4, action="read") read (u, rec=2) v print *, v end program p

    Answer

    22

    Record two holds the second value written.

  4. What happens when a program writes record five of a file that has two records?

    Answer

    It is an error: a direct file cannot have a gap

    A direct file is dense; a missing record is not representable.

  5. What does recl count for a formatted direct file?

    Answer

    Characters of the record

    A formatted record is text, so its length is in characters.

  6. How is a record of a formatted direct file padded?

    Answer

    With blanks to the full record length

    Every record has the same length, so the text is blank-filled.

  7. What does a read past the last record report when IOSTAT is present?

    Answer

    A positive status, and the variables keep their values

    The missing record is a condition the status reports.

  8. What gives the number of records in a direct file of known length?

    Answer

    The file size divided by the record length

    Every record takes the same number of bytes.

  9. Which combination reads record three without reading one and two?

    Answer

    ACCESS="DIRECT" with REC=3

    A direct read takes the record number.

  10. What does a formatted direct file look like in a text editor?

    Answer

    One long line, because the records have no line endings

    The records are fixed blocks of characters with no newline.

The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev