← IGCSE Computer Science Paper 2

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
A program runs OPENFILE 'a.txt' FOR WRITE, then WRITEFILE 'a.txt', 'Red', then WRITEFILE 'a.txt', 'Blue', then CLOSEFILE 'a.txt'. What does the trace table show in the file at the end?
Only Blue, because the second WRITEFILE replaced RedRed on line 1, Blue on line 2
Matches
File after WRITEFILE "A.txt", 5 then 8: only 8
↓
The trace forgot that each WRITEFILE adds an item. After both commands the file holds 5 and 8, not just 8.
Applying
Meanings
Term
trace table
Meaning
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.
Practice
DECLARE Item : STRING
Testing
Gaps
OPENFILE "N.txt" FOR
Check Answer
Spots
2WRITEFILE "a.txt", "Hi"
3CLOSEFILE "b.txt"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Blobfish' badge — Pressure Handler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A program runs OPENFILE 'a.txt' FOR WRITE, then WRITEFILE 'a.txt', 'Red', then WRITEFILE 'a.txt', 'Blue', then CLOSEFILE 'a.txt'. What does the trace table show in the file at the end?
A. Red on line 1, Blue on line 2 ✓
B. Only Blue, because the second WRITEFILE replaced Red
Each WRITEFILE adds a new line after the previous one. Only reopening the file for WRITE removes the old data.
2. File data.txt holds the lines 5, 8, 2. The program runs OPENFILE FOR READ, then READFILE 'data.txt', X once. What is X in the trace table?
A. 5 ✓
B. 8
The first READFILE reads the first line of the file, so X holds 5. The next READFILE would read 8.
3. File scores.txt holds 3 lines. The program then runs OPENFILE 'scores.txt' FOR WRITE, WRITEFILE 'scores.txt', 'New', CLOSEFILE. What is in the file now?
A. The 3 old lines followed by New
B. Only New ✓
Opening a file for WRITE replaces the old contents. In your trace table, empty the file column at that step.
4. A file holds the lines 'Cat' and 'Dog'. The program reads them with READFILE 'f.txt', A and then READFILE 'f.txt', B, then runs OUTPUT B. What is the final OUTPUT?
A. Cat
B. Dog ✓
A gets Cat from the first READFILE and B gets Dog from the second. OUTPUT B shows Dog.
5. When should you update a trace table for a file program?
A. After every file command, recording the file and variable values ✓
B. Only once at the end of the program
A trace table follows the program step by step. You update it after each OPENFILE, READFILE, WRITEFILE and CLOSEFILE.

Match the Pairs

WRITEFILE "Scores.txt", 45 → Adds the value 45 as the next item in the file, so the trace table's file column gains a new entry.
READFILE "Scores.txt", Num → Copies the next unread item from the file into Num, so the trace table shows Num's new value.
Reopening a file for WRITE erases the old data, so the trace table shows the file as empty before the new writes. → OPENFILE "Log.txt" FOR WRITE
The final OUTPUT is found by reading the last value recorded in the variable column of the trace table. → OUTPUT Total
File after WRITEFILE "A.txt", 5 then 8: only 8 → The trace forgot that each WRITEFILE adds an item. After both commands the file holds 5 and 8, not just 8.
READFILE "A.txt", X traced as X = 8 (file holds 5, 8) → The first READFILE reads the first unread item, so X should be 5, not 8.

Key Term

trace table
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.

Fill in the Blanks

1. OPENFILE "N.txt" FOR WRITE // N.txt is now empty: old data erased WRITEFILE "N.txt", "Zoe"
Hint: Type the file mode that makes a trace table show the file emptied when it is reopened.
2. WRITEFILE "N.txt", Name // File now holds the value of Name CLOSEFILE "N.txt"
Hint: Type the command that adds the variable's value to the file, changing the file contents in the trace table.
3. DECLARE Name : STRING READFILE "N.txt", Name OUTPUT Name
Hint: Type the variable that receives the line read, so its trace table value changes after this step.
4. READFILE "N.txt", Name READFILE "N.txt", Name OUTPUT Name
Hint: Type the keyword that displays the variable, giving the final result you find at the end of the trace.
5. CLOSEFILE "N.txt" OPENFILE "N.txt" FOR READ READFILE "N.txt", Name
Hint: Type the file mode that lets the saved data be read back without being erased.

Spot the Error

Question 1
A OPENFILE "a.txt" FOR WRITE
B WRITEFILE "a.txt", "Hi"
C CLOSEFILE "b.txt"
D OPENFILE "a.txt" FOR READ
E READFILE "a.txt", Word
F OUTPUT Word
Line C — 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".
Question 2
A DECLARE Line : STRING
B OPENFILE "n.txt" FOR READ
C READFILE "n.txt", Line
D OUTPUT Lines
E CLOSEFILE "n.txt"
Line D — 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.
Question 3
A OPENFILE "t.txt" FOR WRITE
B WRITEFILE "t.txt", "Red"
C WRITEFILE "Blue", "t.txt"
D CLOSEFILE "t.txt"
Line C — WRITEFILE needs the file name first and then the data. The arguments are swapped. It should be WRITEFILE "t.txt", "Blue".
Question 4
A DECLARE Item : STRING
B OPENFILE "x.txt" READ
C READFILE "x.txt", Item
D OUTPUT Item
Line B — OPENFILE needs the keyword FOR before the mode. Without it the file is never opened, so Item cannot be traced. It should be OPENFILE "x.txt" FOR READ.
Question 5
A OPENFILE "s.txt" FOR READ
B WRITEFILE "s.txt", "Cat"
C CLOSEFILE "s.txt"
Line A — The program uses WRITEFILE on line 2, so the file must be opened in WRITE mode. A file opened FOR READ cannot be written to. It should be OPENFILE "s.txt" FOR WRITE.

Student with a class code? Join your class →