Tracing a file handling program
A table that follows a program step by step, recording the file contents and each variable's value after every file command, until you find the final OUTPUT.
Understanding Tracing a file handling program in IGCSE Computer Science Paper 2
What it does: When tracing, each OPENFILE ... FOR WRITE replaces the file's old contents, so you must clear the file column at that step. The OUTPUT line prints the variable's current value, which you take from the last entry for that variable in the trace table. The first READFILE reads the first line of the file, so X holds 5. The next READFILE would read 8. A gets Cat from the first READFILE and B gets Dog from the second. OUTPUT B shows Dog. A holds 4 and B holds 7. They are INTEGER values, so A + B is added to give 11 rather than joined together.
Common mistakes: CLOSEFILE must use the same file name that was opened. "b.txt" was never opened, so a.txt stays open and the trace is wrong. It should be CLOSEFILE "a.txt". The value read from the file is stored in the variable Line. Lines is not declared and holds nothing in the trace table. It should be OUTPUT Line.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 500–522) and word search (Modules 500–538).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
trace tableFill in the Blanks
Spot the Error
Student with a class code? Join your class →