File Handling
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.
| Operation | What 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
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.
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.
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.
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:
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:
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.
- 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 WRITEdestroys its contents. To keep them, openFOR 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
EOFfunction 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 EOFis A Level syntax and will not be credited here.