Skip to content
IGCSE·Tuition
Computer Science · Lesson

Handle end-of-file when reading a text file

File code feels harder than a normal loop because you cannot see the data, only the lines that read it.

On this page
  1. What is the pattern?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

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
StepEOF?MarkTotalCount
Before loop00
Test 1FALSE00
After body 112121
Test 2FALSE121
After body 27192
Test 3FALSE192
After body 320393
Test 4TRUE393

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.

Questions people ask

What does end-of-file mean?

It is the point after the last line has been read, when nothing is left in the file. A test such as EOF returns true at that point, so a loop can stop. Until then it returns false and the loop reads another line.

Why must READFILE be inside the loop?

Each read moves the position forward. If the read is missing from the loop body, the position never changes, the end-of-file test never becomes true and the loop never stops.

Do I need to close a file?

Yes. Closing the file after the loop releases it, and exam answers are expected to show it. Place the close statement after the loop ends, not inside the loop, or the next pass would try to read a closed file.

Updated:

Your next step

If you cannot tell whether a file loop will stop or run forever, a one-to-one teacher can trace it with you using a small file until the pattern is automatic.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students