8.3 File handling

Show All Section Notes
Test yourself on File handling 14 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

File Handling

Watch this lesson Video 8.3.1 · 6:18 · Making data survive the program closing — in the pseudocode the paper uses

1. Why Files Are Needed

Variables live in RAM, which is volatile — when the program ends, everything it was holding is gone. A file stores data in secondary storage, which is non-volatile, so the data survives the program closing and can be loaded again next time.

That is the whole reason files exist, and it connects directly to topic 3.3: primary memory is fast but temporary, secondary storage is slower but permanent. A program that must remember anything between runs has to write it to a file.

2. The Four Pseudocode Operations

The exam uses pseudocode, and there are four file operations to know. Learn these as the primary form — the Python further down is for the practical paper.

OperationWhat it does
OPENFILE <name> FOR READ Opens an existing file so data can be read from it.
OPENFILE <name> FOR WRITE Opens a file so data can be written to it. If the file already exists its contents are erased.
OPENFILE <name> FOR APPEND Opens a file so new data is added to the end, keeping what is already there.
READFILE <name>, <variable> Reads the next line from the file into the variable.
WRITEFILE <name>, <data> Writes a line of data to the file.
CLOSEFILE <name> Closes the file, saving changes and releasing it.

Writing to a file

// Create the file and write three names OPENFILE "names.txt" FOR WRITE WRITEFILE "names.txt", "Alice" WRITEFILE "names.txt", "Bob" WRITEFILE "names.txt", "Chi" CLOSEFILE "names.txt"

Reading from a file

Reading takes more thought than writing, because the program has to know when to stop. IGCSE pseudocode gives you no way to ask whether the end of a file has been reached, so the stopping condition has to come from somewhere else. There are two standard ways.

Method 1 — a known number of lines

If the question tells you how many records the file holds, a counting loop is the natural fit.

// The file holds exactly three names OPENFILE "names.txt" FOR READ FOR Index ← 1 TO 3 READFILE "names.txt", Name OUTPUT Name NEXT Index CLOSEFILE "names.txt"

Method 2 — a rogue value on the last line

When the number of lines is not known, the file itself carries a marker on the last line — a value that could never be real data. The loop reads until it meets that value.

// The last line of the file is the rogue value "XXX" OPENFILE "names.txt" FOR READ READFILE "names.txt", Name // read the first line WHILE Name <> "XXX" DO OUTPUT Name READFILE "names.txt", Name // read the next line ENDWHILE CLOSEFILE "names.txt"
⚠️ There is no EOF in 0478. You will see WHILE NOT EOF(...) in A Level material and in most real programming languages, but EOF is not part of the IGCSE pseudocode set — it belongs to the A Level 9618 syllabus. On this paper, end a read loop with a count or with a rogue value. Reaching for EOF here is reaching for the wrong syllabus.
Why two READFILE statements in method 2? The first one gives the WHILE condition something to test before the loop starts. The one at the foot of the loop fetches the next line, so the condition can change. Leave either out and the loop reads nothing, or never ends. It is the rogue-value shape from topic 7, applied to a file.

3. Reading and Processing Together

Most questions combine file reading with a standard method from topic 7. Here the file holds exam marks, one per line, ending with the rogue value -1. The algorithm totals them and counts them:

Total ← 0 Count ← 0 OPENFILE "marks.txt" FOR READ READFILE "marks.txt", Mark WHILE Mark <> -1 DO Total ← Total + Mark Count ← Count + 1 READFILE "marks.txt", Mark ENDWHILE CLOSEFILE "marks.txt" IF Count > 0 THEN OUTPUT "Average: ", Total / Count ENDIF

Notice the familiar shape: the two initialisations sit before the loop, the accumulation happens inside it, and the division happens after it — guarded, in case the file held nothing but the rogue value. Note too that -1 is never added to the total: the loop exits the moment it is read.

4. Python Equivalent

For the practical paper, the same operations in Python:

PYTHON — practical paper only Writing
file = open("names.txt", "w")
file.write("Alice\n")
file.write("Bob\n")
file.close()
Reading
file = open("names.txt", "r")
for line in file:
    print(line.strip())
file.close()

Python's "w" is FOR WRITE, "r" is FOR READ, and "a" is FOR APPEND. The .strip() removes the newline character that write added.

⚠️ Exam Focus
  • Use pseudocode on the written paper. Python may not be credited where the question asks for pseudocode, and the keywords above are what the mark scheme is written around.
  • Opening for WRITE erases the file. This is a favourite question: opening an existing file FOR WRITE destroys its contents. To keep them, open FOR APPEND.
  • Always close the file. Closing saves the changes and releases the file. An answer that opens and never closes loses a mark.
  • There is no EOF function in 0478. End a read loop with a counting loop when the number of lines is known, or with a rogue value when it is not. WHILE NOT EOF is A Level syntax and will not be credited here.