Pro Content

Upgrade to access all Cambridge 9618 lessons including file organisation, access methods and Cambridge pseudocode file operations.

Upgrade to Pro →
← Back to Dashboard
📘 Paper 3 · 3.1 Data Representation
3.1.2 File Organisation & Access
Cambridge 9618 · International A Level Computer Science · ~18 min read
Notes
Video
Slides
Quiz
Worksheet

Three Types of File Organisation

How records are physically stored in a file determines how they can be accessed. Cambridge 9618 requires you to know three methods:

📄
Serial
Records in arrival order — no sorting. Must read every record to find one. Used for logs, batch files, transaction queues.
🗂️
Sequential
Records sorted by a key field. Still must read from beginning to find a record, but binary search possible on sorted media. Used for payroll, master files.
Random (Direct)
Record location calculated from key field using a hashing algorithm. Jump directly to any record in O(1). Used for real-time databases.

1. Serial File Organisation

Records are stored in the order they were written — no sorting whatsoever. The only way to find a specific record is to read from the beginning until you reach it. This is also called sequential access when the medium enforces it (e.g. magnetic tape).

Smith, 101
Jones, 047
Brown, 209
Ali, 315
Taylor, 052
To find "Brown, 209" — must read records 1, 2, then 3. Average n/2 reads. Worst case n reads.
  • Advantages: Simple to implement; no sorting overhead; fast to write new records (just append)
  • Disadvantages: Slow to search (O(n) — must read all records in worst case); cannot jump to a specific record
  • Suitable for: Transaction logs, temporary files, batch processing where all records are processed in order

2. Sequential File Organisation

Records are stored in order of a key field (e.g. student ID, account number). The key field must be unique and used to sort records when they are written. Sequential files on disk allow skipping forward (unlike true serial tapes).

ID: 047
ID: 052
ID: 101
ID: 209
ID: 315
Records sorted by ID. Binary search: start at 101 (mid), 209 > 101, look right → found at ID:209. Fewer reads needed than serial.
  • Advantages: Faster searching than serial on sorted keys; more organised; batch updates are efficient (both files in order)
  • Disadvantages: Inserting/deleting records requires rewriting the whole file; still O(n) for linear search; O(log n) for binary search only with random access disk
  • Suitable for: Payroll files, master files updated periodically, any scenario where records are processed in key order

3. Random (Direct) Access File Organisation

A hash function is applied to the key field to calculate the exact location (record address) where the record is stored. This allows jumping directly to any record without reading others — O(1) average access time.

// Simple hash: address = key MOD maxRecords
address ← 209 MOD 100  // = 9 → stored at slot 9
address ← 315 MOD 100  // = 15 → stored at slot 15
address ← 101 MOD 100  // = 1 → stored at slot 1

// Collision: 201 MOD 100 = 1 (same as 101) → need collision handling
// e.g. linear probing: try slot 2, 3, ... until empty slot found
  • Advantages: Very fast access (O(1)) for individual records; supports insertion and deletion of individual records efficiently
  • Disadvantages: Wasted space if hash table not full; hash collisions require handling; difficult to read records in sorted order
  • Suitable for: Real-time systems, databases requiring instant record retrieval, airline/hotel booking systems

File Operations in Cambridge 9618 Pseudocode

Cambridge 9618 has specific pseudocode for file operations. You must know the exact commands:

OPENFILE f FOR READ
Opens file f for reading. File pointer at start.
OPENFILE f FOR WRITE
Opens file f for writing (creates new or overwrites).
OPENFILE f FOR APPEND
Opens file f to write new records at the end.
OPENFILE f FOR RANDOM
Opens file for random (direct) access.
READFILE f, variable
Reads next record from f into variable.
WRITEFILE f, data
Writes data to the next position in f.
CLOSEFILE f
Closes file f (flushes buffer, releases handle).
EOF(f)
Returns TRUE if end of file f has been reached.
SEEK f, address
Moves the file pointer to the specified record address (random access files only).
GETRECORD f, variable
Reads record at current file pointer position (random file).
PUTRECORD f, variable
Writes a record at current file pointer position (random file).

Reading a serial/sequential file until EOF

DECLARE studentName : STRING
OPENFILE "students.dat" FOR READ

WHILE NOT EOF("students.dat") DO
  READFILE "students.dat", studentName
  OUTPUT studentName
ENDWHILE

CLOSEFILE "students.dat"

Writing to a serial file

OPENFILE "log.dat" FOR WRITE  // creates new file
WRITEFILE "log.dat", "Entry 1"
WRITEFILE "log.dat", "Entry 2"
CLOSEFILE "log.dat"

// To add to existing file without overwriting:
OPENFILE "log.dat" FOR APPEND
WRITEFILE "log.dat", "Entry 3"
CLOSEFILE "log.dat"

Random access file — reading and updating a specific record

DECLARE searchKey : INTEGER
DECLARE address : INTEGER
DECLARE rec : Student

searchKey ← 209
address ← searchKey MOD 100  // hash function

OPENFILE "students.dat" FOR RANDOM
SEEK "students.dat", address
GETRECORD "students.dat", rec

// Update and write back
rec.grade ← 'A'
SEEK "students.dat", address  // move pointer back
PUTRECORD "students.dat", rec
CLOSEFILE "students.dat"

Comparison Table

FeatureSerialSequentialRandom
Record orderArrival orderSorted by keyHashed position
Access methodSequential onlySequential onlyDirect (random)
Search speedO(n)O(n) linear / O(log n) binaryO(1) average
Insert recordAppend to endRewrite whole fileCalculate address, write
Delete recordRewrite fileRewrite fileMark record as deleted
Storage efficiency100%100%<100% (empty slots)
Typical useLogs, transactionsPayroll, batchReal-time databases
Cambridge 9618 exam tip: Know all the file operation keywords exactly — OPENFILE/CLOSEFILE, READFILE/WRITEFILE, EOF(), SEEK, GETRECORD, PUTRECORD. A common exam question gives a scenario and asks you to choose serial, sequential or random and justify your choice. Always explain in terms of whether individual record access is needed, whether records need processing in key order, and whether real-time speed matters.
⚠️ Common Mistakes
  • Confusing serial and sequential — serial is arrival order (unsorted); sequential is sorted by key
  • Using WRITE instead of APPEND when adding records to an existing file — WRITE overwrites the whole file
  • Forgetting CLOSEFILE — the file must always be closed after use to flush the buffer and release the file handle
  • Using READFILE instead of GETRECORD for random files (READFILE is for sequential; GETRECORD is for random)
  • Not using SEEK before GETRECORD/PUTRECORD — the file pointer must be positioned first
  • Claiming sequential files allow O(1) access — they don't; only random/direct files achieve O(1) lookup
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 3.1.2 File Organisation & Access

8 questions · Cambridge 9618 standard

Q1State the difference between serial and sequential file organisation.[2]
✅ Mark scheme
Serial files store records in the order they are written (arrival order) — there is no sorting [1]; sequential files store records sorted by a key field in ascending or descending order [1].
Q2Write pseudocode to open a file "scores.dat" for reading, read all records (each a string) outputting each one, then close the file.[5]
✅ Mark scheme
DECLARE score : STRING [1]; OPENFILE "scores.dat" FOR READ [1]; WHILE NOT EOF("scores.dat") DO [1]; READFILE "scores.dat", score; OUTPUT score [1]; ENDWHILE; CLOSEFILE "scores.dat" [1].
Q3A hospital stores patient records sorted by patient ID. Explain whether serial, sequential or random file organisation is most appropriate for: (a) a batch processing run that processes all patients each night; (b) a receptionist who needs to instantly retrieve any patient's record during a call.[4]
✅ Mark scheme
(a) Sequential — records are sorted by patient ID so the batch run can process all patients in order without needing to jump around the file; efficient for processing all records in key sequence [2]; (b) Random — the receptionist needs instant access to any individual patient record (O(1) via hash); sequential would require reading from the start each time [2].
Q4A random access file uses hash function: address = key MOD 50. Calculate the address for key values 87, 50, 137. Identify any collision and explain how linear probing resolves it.[4]
✅ Mark scheme
87 MOD 50 = 37; 50 MOD 50 = 0; 137 MOD 50 = 37 [1 per correct address, max 2]; collision: 87 and 137 both hash to address 37 [1]; linear probing: when writing 137, address 37 is occupied, so try 38 — if empty, store at 38; continue to 39, 40, etc. until an empty slot is found [1].
Q5What is the difference between OPENFILE "f" FOR WRITE and OPENFILE "f" FOR APPEND?[2]
✅ Mark scheme
OPENFILE FOR WRITE creates a new file (or overwrites existing content from the beginning) — all previous data is lost [1]; OPENFILE FOR APPEND opens the existing file and positions the file pointer at the end, so new records are added after existing ones without overwriting [1].
Q6Write pseudocode to: open a random access file "accounts.dat", seek to address 25, read the record into a variable acc (of type Account), update acc.balance by adding 500, then write the record back to address 25.[5]
✅ Mark scheme
DECLARE acc : Account [1]; OPENFILE "accounts.dat" FOR RANDOM [1]; SEEK "accounts.dat", 25; GETRECORD "accounts.dat", acc [1]; acc.balance ← acc.balance + 500 [1]; SEEK "accounts.dat", 25; PUTRECORD "accounts.dat", acc; CLOSEFILE "accounts.dat" [1].
Q7A file stores employee records with EmployeeID as the key field. The file uses random (direct) access with a hash function h(k) = k MOD 50 to calculate the home address. EmployeeID 173 is requested. Calculate the home address. If that slot is occupied, describe how the system locates the record using linear probing.[4]
✅ Mark scheme
Home address = 173 MOD 50 = 23 [1]; Read record at address 23; if EmployeeID ≠ 173, probe next address: 24 [1]; continue probing 25, 26, … (address + 1 each time) until EmployeeID = 173 is found or an empty slot is encountered (record not present) [1]; wraps around to 0 after address 49 if needed [1].
Q8Compare serial, sequential, and random file organisation. For each, state: the order records are stored, whether a key field is required, and give one application where that organisation is most appropriate.[6]
✅ Mark scheme
Serial: records stored in order of arrival / no particular order [1]; no key field required [1]; suitable for transaction logs or backup files where order of writing matters [1]; Sequential: records stored in key field order [1]; key field required [1]; suitable for batch processing e.g. payroll where all records are processed in order [1]; Random (direct): records stored at address calculated from key via hash function [1]; key field required [1]; suitable for real-time systems e.g. airline reservations where a single record must be retrieved instantly [1]. Award max 6.
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 3.1.2 File Organisation

10 questions · 10 marks · 10 minutes

← 3.1.1 User-Defined Types
52 of 82 · Cambridge 9618
3.1.3 Floating Point →