Reading a file has a fixed pattern: open the file, keep reading until end-of-file, close the file. The loop does not know how many lines there are, so it asks a question each time: “has the file run out?”
This lesson belongs to searching, sorting and files. It uses the loop skills from repetition and arrays and the line-handling from reading a record. Check the Cambridge pseudocode guide for your exam year for the exact statement forms.
What is the pattern?
OPENFILE "Marks.txt" FOR READ
WHILE NOT EOF("Marks.txt")
READFILE "Marks.txt", Mark
...use Mark...
ENDWHILE
CLOSEFILE "Marks.txt"
The test sits at the start of the loop, so it is checked before each read. The read sits inside the loop body, because each read moves to the next line.
Worked example
A fictional file Marks.txt holds three lines, each a whole number: 12, then 7, then 20. This program totals them and works out the average.
Total ← 0
Count ← 0
OPENFILE "Marks.txt" FOR READ
WHILE NOT EOF("Marks.txt")
READFILE "Marks.txt", Mark
Total ← Total + Mark
Count ← Count + 1
ENDWHILE
CLOSEFILE "Marks.txt"
OUTPUT Total / Count
| Step | EOF? | Mark | Total | Count |
|---|---|---|---|---|
| Before loop | 0 | 0 | ||
| Test 1 | FALSE | 0 | 0 | |
| After body 1 | 12 | 12 | 1 | |
| Test 2 | FALSE | 12 | 1 | |
| After body 2 | 7 | 19 | 2 | |
| Test 3 | FALSE | 19 | 2 | |
| After body 3 | 20 | 39 | 3 | |
| Test 4 | TRUE | 39 | 3 |
At test 4 the file has no lines left, so the loop ends. The output is 39 / 3 = 13. Check: 12 + 7 + 20 = 39, and 39 divided by 3 is 13.
The mistake to watch for
A student forgets the read:
WHILE NOT EOF("Marks.txt")
Total ← Total + Mark
Count ← Count + 1
ENDWHILE
Nothing inside the loop reads a line, so the file position never moves. EOF stays FALSE, and the loop never ends. Also Mark was never given a value from the file.
The correction is to put READFILE inside the body, before using Mark. When you trace a file loop, always ask: “what in this body makes the test eventually true?” The restricted pseudocode trace trainer stops such a loop at a safety limit. It shows the variables that never changed, so the fault is visible.
A second slip is putting CLOSEFILE inside the loop. The first pass would close the file and the second would try to read from it.
Check yourself
1. A file holds the lines 4, 9, 6. Using the program above, what are Total and Count at the end?
Show answer
4 + 9 + 6 = 19, and the body runs once per line. Total = 19, Count = 3.
2. A file has five lines. How many times does the loop body run, and how many times is the EOF test evaluated?
Show answer
The body runs 5 times. The EOF test is evaluated 6 times: five times false, then once true.
3. Where should CLOSEFILE go, and why?
Show answer
After ENDWHILE. It runs once when all reading is finished. Inside the loop it would close the file after the first read.
Where this leads next
The last lesson in the module asks how to check an algorithm against empty and duplicate data. It also reuses this file loop, because an empty file makes Count zero.
If file questions cost you marks because of small ordering slips, our teachers can work on them in online one-to-one Computer Science tuition.