Why store data in a file? Two reasons
A file gives persistent storage. Variables and arrays live in volatile RAM, so their contents are lost when the program ends or the power is off. A file sits on non-volatile secondary storage (disk/SSD), so it survives. Examiners want two distinct reasons: (1) permanent storage — data is not lost when the program stops; (2) reuse — the program can read it back on a later run, or share it with another program. One reason scores half.
Cambridge file-handling statements
Cambridge pseudocode has fixed file-handling statements. OPENFILE <file> FOR READ opens to read; OPENFILE <file> FOR WRITE opens to write and creates a new file, erasing existing contents. READFILE <file>, <var> reads the next line into a var; WRITEFILE <file>, <var> writes one; EOF(<file>) returns TRUE at end of file; CLOSEFILE <file> closes it. Open before use and close after; the filename is a string and a file is open in one mode at a time.
Read every line with a WHILE NOT EOF loop
To process every record, read in a loop controlled by EOF:
OPENFILE "Scores.txt" FOR READ
WHILE NOT EOF("Scores.txt")
READFILE "Scores.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "Scores.txt"
WHILE NOT EOF checks before each read; each READFILE takes the next line, so it must sit inside the loop or the loop never ends. To add data instead, open FOR WRITE, WRITEFILE each item, then CLOSEFILE.
Drawn from real examiner reports.
OPENFILE prepares — READFILE retrieves
Learners conflate opening a file with reading it. OPENFILE ... FOR READ only prepares/locates the file — it retrieves no data. READFILE <file>, <var> is the statement that actually reads a data item into a variable. So the command that retrieves data is READFILE, not OPENFILE — opening a file but never READFILE-ing reads nothing.
November 2023 (w23) examiner report, keyword-matching item: for "retrieves data from a file" quite a few candidates indicated "open" instead of "read". OPENFILE vs READFILE is the recurring confusion.
Missing OPENFILE/CLOSEFILE or wrong order
Marks reward the complete, ordered sequence: OPENFILE in the right mode before any read/write, then READFILE/WRITEFILE, then CLOSEFILE after. Frequent slips: no OPENFILE; no CLOSEFILE (the dropped final mark); wrong mode — FOR READ then WRITEFILE; or inputting a value but forgetting the WRITEFILE that stores it.
June 2023 (s23) examiner report, Paper 2 file-handling item: candidates who correctly opened a file, wrote the input data and then closed it gained the marks, but many found this challenging — the open, write, close sequence (and remembering CLOSEFILE) was the differentiator.
Giving only one reason for using a file
"Why store data in a file?" needs two distinct reasons, not one. Candidates write "to store data permanently" and stop. Bank a second idea: the data can be read back / reused on a later run, or shared between programs or computers. Persistence and reuse — one without the other scores only half the marks.
November 2023 (w23) examiner report, "purpose of a file" item: most candidates stated one reason (to permanently store data) but most did not give a second reason. Persistence plus reuse are both needed.
READFILE outside the loop = infinite loop
Two read-loop slips. (1) The READFILE must sit inside the WHILE NOT EOF loop — outside it, the same value repeats and EOF is never reached, an infinite loop. (2) The condition is WHILE NOT EOF(<file>); WHILE EOF(...) reads nothing, as the file is not at its end at the start. EOF takes the filename and the file must be open FOR READ.
FOR WRITE erases the whole file
Opening a file FOR WRITE creates a new file and deletes any existing contents — use it only when you intend to overwrite; to keep old data, open FOR READ. A file is also open in only one mode at a time: opening FOR READ then trying to WRITEFILE (or the reverse) fails. If you must switch modes, CLOSEFILE first, then reopen in the other mode.
A variable in RAM is lost; a file persists
A variable or array lives in volatile RAM and is lost when the program ends or the power is removed; a file lives on non-volatile secondary storage and persists. That is the whole reason to write data to a file: a variable cannot "save" data between runs, and a file is chosen for persistence (and reuse), not speed.
Write the OPEN, USE, CLOSE skeleton first
Write the fixed skeleton first, then fill the middle:
OPENFILE <file> FOR <READ or WRITE>
... READFILE / WRITEFILE ...
CLOSEFILE <file>
Pick the mode (out -> READ; in -> WRITE, which overwrites) and always finish with CLOSEFILE.
Read all records = WHILE NOT EOF loop
When a question says read every record, use the EOF loop: open FOR READ, WHILE NOT EOF(<file>) with a READFILE inside it, process each item, then CLOSEFILE. It reads any number of records without knowing the count in advance — no separate counter needed.
Match the command word
Answer at the grain asked. "State/name the command" wants the keyword only (e.g. READFILE). "Write pseudocode" wants the full open, use, close routine. "Give two reasons" wants two distinct points — persistence and reuse. The wrong grain wastes marks.
This sub-topic has two parts: why a program stores data in a file, and how to open, close, read from and write to a file using Cambridge pseudocode. Paper 2 questions are graded on the logic of your pseudocode, so learn the exact keywords and the open -> use -> close skeleton.
File — a collection of data stored on secondary storage (e.g. a hard disk or SSD) under a name, so it can be kept after a program ends and reused later.
Purpose of storing data in a file (two reasons):
Full notes, flashcards, Q&A and the topic quiz for every premium subject.
Premium plans are US$8.99/month or US$49.99/year — first month free.
Studying with a parent's blessing? Show them this.