b flag.b (e.g. rb, wb) in **all** languages.Cambridge expects candidates to be able to open, read/write, and close a file, handling errors where necessary. The steps below follow the order in which an exam‑style algorithm is written.
SEEK / TELL for random (direct) access.
OPEN "students.txt" AS studentFile MODE "r+" // r+, w, a, etc.
IF studentFile = NIL THEN
DISPLAY "Cannot open file – check name and permissions"
STOP
END IF
The syntax above matches the Cambridge pseudocode format OPEN <filename> AS <handle> MODE <mode>.
READ studentID FROM studentFile // reads the next item (e.g. an integer) WRITE newGrade TO studentFile // writes a single item
READLINE line FROM studentFile // reads up to the next line‑break WRITELINE line TO studentFile // writes the line and adds a line‑break
READBLOCK buffer[100] FROM imageFile // reads 100 bytes into buffer WRITEBLOCK buffer[100] TO imageFile // writes 100 bytes from buffer
SEEK(handle, offset, origin) – moves the pointer to offset bytes from origin (BEGINNING, CURRENT, or END).TELL(handle) – returns the current byte position; useful for calculating offsets in random access.Example (random access to a fixed‑length record):
offset ← (recordNumber‑1) * recordLength SEEK studentFile, offset, BEGINNING READ record FROM studentFile
CLOSE studentFile
In real languages this is often done with try … finally or a with statement to guarantee the file is closed even if an error occurs.
| Mode | Description | Typical Use‑case (most suitable) |
|---|---|---|
r |
Read‑only. File must already exist. | Reading a configuration or data file. |
w |
Write‑only. Creates a new file or truncates an existing one. | Saving a final report that replaces any old version. |
a |
Append‑only. Writes are added to the end of the file. | Log files where old entries must be preserved. |
r+ |
Read‑write. File must exist; pointer starts at the beginning. | Updating a specific record in a data file. |
w+ |
Read‑write. Creates a new file or truncates an existing one. | Temporary files that will be rewritten during the program. |
a+ |
Read‑append. Reads from start; writes are always appended. | Reading a log while still adding new entries. |
rb, wb, ab, … |
Binary equivalents of the text modes (add b). |
Working with images, audio, or any non‑text data. |
SEEK(). Allows immediate read/write of a particular record.Example: A school database contains 10 000 student records, each 32 bytes long. To display the record for student #7 532, compute the offset 7 531 × 32 = 240 992 bytes and SEEK directly to that position.
When reading sequentially, the program must know when the file ends.
WHILE NOT EOF(handle) DO … END WHILEfor line in file: automatically stops at EOF.while (fgets(buf, size, fp) != NULL) – the function returns NULL at EOF.r mode and the file does not exist.Good practice: always close the file, even if an error occurs. In many languages this is done with a try … finally (or with) construct.
TRY
OPEN "students.txt" AS f MODE "r+"
IF f = NIL THEN
DISPLAY "Cannot open file – check name and permissions"
STOP
END IF
WHILE NOT EOF(f) DO
READLINE line FROM f
fields ← SPLIT(line, ",")
IF fields[0] = targetID THEN
fields[3] ← newGrade
SEEK f, -LENGTH(line), CURRENT // back to start of this line
WRITELINE JOIN(fields, ",") TO f
EXIT WHILE
END IF
END WHILE
FINALLY
IF f ≠ NIL THEN CLOSE f END IF
END TRY
Step‑by‑step algorithm that follows the four‑step scaffold.
OPEN "students.txt" AS stuFile MODE "r+"
IF stuFile = NIL THEN
DISPLAY "Error opening file"
STOP
END IF
found ← FALSE
WHILE NOT EOF(stuFile) DO
READLINE line FROM stuFile
fields ← SPLIT(line, ",")
IF fields[0] = targetID THEN
fields[3] ← newGrade // change the grade field
SEEK stuFile, -LENGTH(line), CURRENT // back to start of this line
WRITELINE JOIN(fields, ",") TO stuFile
found ← TRUE
EXIT WHILE
END IF
END WHILE
IF NOT found THEN
DISPLAY "Student ID not in file"
END IF
CLOSE stuFile
import datetime
# The “with” statement guarantees the file is closed even on error
with open("log.txt", "a", encoding="utf-8") as log_file:
log_file.write(f"Program started at {datetime.datetime.now()}")
For each scenario choose the *most appropriate* file‑access mode and give a brief justification (1‑2 sentences).
w. It creates a new file (or truncates an existing one) so the old mark‑sheet is overwritten.a. Append mode adds new records to the end without disturbing existing data.r+. The file must exist; read‑write access lets the program read the config and later rewrite the changed line.rb. Binary read‑only mode prevents accidental modification and ensures the exact byte pattern of each image is preserved.w will erase existing data, a will never overwrite, and r+ is needed for updating existing records.OPEN succeeded (handle ≠ NIL) before attempting any read or write.WHILE NOT EOF(handle) (or language‑specific equivalents) to avoid infinite loops.b flag in every language.SEEK/TELL is faster when only a few records are needed.try … finally or with construct, to ensure data is flushed and resources are released.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.