CIE AS & A Level Computer Science, section 13.2 (File organisation and access)
Methods of access
| Access | How records are read | Suitable for |
|---|---|---|
| Sequential | start at the beginning and read each record in turn until the one needed is found | serial and sequential files |
| Direct | go straight to the required record using its calculated address | random files (and sequential files with an index) |
Sequential access
To find a record in a serial file, every record must be read until it is found, or the end of the file is reached. In a sequential (sorted) file you can stop early once you pass the place where the key would be.
OPENFILE "Customers.dat" FOR READ
Found ← FALSE
WHILE NOT EOF("Customers.dat") AND Found = FALSE
READFILE "Customers.dat", Record
IF Record.ID = Target THEN
Found ← TRUE
ENDIF
ENDWHILE
CLOSEFILE "Customers.dat"
Direct access
For a random file, the key is hashed to give the address, and the record is read from there without reading earlier ones. In pseudocode, SEEK moves to an address, and GETRECORD and PUTRECORD read and write a record.
OPENFILE "Customers.dat" FOR RANDOM
SEEK "Customers.dat", Address
GETRECORD "Customers.dat", Record
CLOSEFILE "Customers.dat"
Comparison
Direct access is much faster for finding one record in a big file, but needs the file to be organised for it. Sequential access is simple, and best when every record will be processed.
Common mistake: saying direct access reads "every record". It reads only the one needed. Also, even in a sorted sequential file, access is still sequential unless an index or hashing is used.
Exam tip: choose the access method to match the task: all records (sequential) or one record quickly (direct). Mention how the address is found in direct access.
Check yourself
Why is direct access faster for finding one record?
It goes straight to the record’s address, without reading the records before it.
Which file organisation allows direct access?
A random file, where the position is calculated from the key.
How can a search of a sequential file stop early?
Because the file is sorted, the search can stop once a key larger than the target has been read.