← IGCSE Computer Science Paper 2

Common mistakes in file handling pseudocode

A mistake such as using the wrong mode, missing OPENFILE or CLOSEFILE, using different file names, or opening a file FOR WRITE and wiping its old data.

Understanding Common mistakes in file handling pseudocode in IGCSE Computer Science Paper 2

What it does: This code opens the file first and only then reads from it, which avoids the error of using a file before OPENFILE. CLOSEFILE after the last WRITEFILE makes sure the data is saved, avoiding the mistake of forgetting CLOSEFILE. READFILE needs the file to be opened FOR READ. A file opened FOR WRITE can only have data written to it, and opening it FOR WRITE also wipes its old data. The same file name must be used in OPENFILE, READFILE and CLOSEFILE, so READFILE should use "marks.txt".

Common mistakes: The program reads from the file, so it must be opened FOR READ. Opening it FOR WRITE would wipe the old data, and READFILE cannot be used on a file opened for WRITE. The file identifier in CLOSEFILE must match the one used in OPENFILE. "log.text" is a different file, so log.txt is never closed. It should be CLOSEFILE "log.txt".

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
Which program reads a line from a file correctly?
OPENFILE "notes.txt" FOR READ READFILE "notes.txt", LineOPENFILE "notes.txt" FOR WRITE READFILE "notes.txt", Line
Matches
OPENFILE "marks.txt" FOR READ READFILE "mark.txt", Mark
↓
The file identifier in READFILE (mark.txt) is different from the one in OPENFILE (marks.txt), so the file names do not match
Applying
Meanings
Term
file handling error
Meaning
A mistake such as using the wrong mode, missing OPENFILE or CLOSEFILE, using different file names, or opening a file FOR WRITE and wiping its old data.
Practice
DECLARE Line : STRING
Testing
Gaps
OPENFILE "names.txt" FOR
Check Answer
Spots
1OPENFILE "data.txt" FOR WRITE
2READFILE "data.txt", Line

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

The 'Peregrine Falcon' badge — Operator Ace, 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. Which program reads a line from a file correctly?
A. OPENFILE "notes.txt" FOR READ READFILE "notes.txt", Line ✓
B. OPENFILE "notes.txt" FOR WRITE READFILE "notes.txt", Line
READFILE needs the file to be opened FOR READ. A file opened FOR WRITE can only have data written to it, and opening it FOR WRITE also wipes its old data.
2. Which program uses the file at the right time?
A. OPENFILE "scores.txt" FOR READ READFILE "scores.txt", Score ✓
B. READFILE "scores.txt", Score OPENFILE "scores.txt" FOR READ
A file must be opened before you use it. READFILE before OPENFILE is an error because the file is not open yet.
3. Which program finishes its file handling properly?
A. OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", "Done"
B. OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", "Done" CLOSEFILE "log.txt" ✓
Forgetting CLOSEFILE is a common mistake. Closing the file makes sure the data is saved and the file is released.
4. Which program uses the same file identifier throughout?
A. OPENFILE "names.txt" FOR READ READFILE "names.txt", Name ✓
B. OPENFILE "names.txt" FOR READ READFILE "name.txt", Name
The file identifier must match exactly in OPENFILE and READFILE. "name.txt" is a different file from "names.txt", so this causes an error.
5. A file already holds 50 high scores that must be kept. Which line is safe if you only want to read them?
A. OPENFILE "scores.txt" FOR WRITE
B. OPENFILE "scores.txt" FOR READ ✓
Opening a file FOR WRITE replaces its old data with a new empty file, so the 50 scores would be lost. Use FOR READ to look at data without changing it.

Match the Pairs

OPENFILE "scores.txt" FOR WRITE READFILE "scores.txt", Score → Error: READFILE is used on a file opened for WRITE, so the program cannot read from it
OPENFILE "names.txt" FOR WRITE WRITEFILE "names.txt", Name → Error: the file is opened FOR WRITE, which wipes any old data in names.txt before the new data is saved
Every file must be opened with OPENFILE before READFILE or WRITEFILE can use it → OPENFILE "data.txt" FOR READ READFILE "data.txt", Item
A file should be closed with CLOSEFILE once the program has finished using it → WRITEFILE "log.txt", Line CLOSEFILE "log.txt"
OPENFILE "marks.txt" FOR READ READFILE "mark.txt", Mark → The file identifier in READFILE (mark.txt) is different from the one in OPENFILE (marks.txt), so the file names do not match
OPENFILE "diary.txt" FOR WRITE WRITEFILE "diary.txt", Entry // program ends → CLOSEFILE is missing, so the file is never closed and the data may not be saved properly

Key Term

file handling error
A mistake such as using the wrong mode, missing OPENFILE or CLOSEFILE, using different file names, or opening a file FOR WRITE and wiping its old data.

Fill in the Blanks

1. OPENFILE "names.txt" FOR READ READFILE "names.txt", Name CLOSEFILE "names.txt"
Hint: Type the file mode that must be used before you can read data from a file.
2. OPENFILE "scores.txt" FOR READ READFILE "scores.txt", Score CLOSEFILE "scores.txt"
Hint: Type the command that must come before any data is read from the file.
3. OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", Message CLOSEFILE "log.txt"
Hint: Type the command that must end the file handling so the file is closed properly.
4. OPENFILE "marks.txt" FOR READ READFILE "marks.txt", Mark CLOSEFILE "marks.txt"
Hint: Type the file name inside the quotes. It must match the name used in OPENFILE.
5. // Old data is lost after this line OPENFILE "best.txt" FOR WRITE WRITEFILE "best.txt", NewScore
Hint: Type the file mode that wipes the old contents of an existing file.

Spot the Error

Question 1
A OPENFILE "data.txt" FOR WRITE
B READFILE "data.txt", Line
C OUTPUT Line
D CLOSEFILE "data.txt"
Line A — The program reads from the file, so it must be opened FOR READ. Opening it FOR WRITE would wipe the old data, and READFILE cannot be used on a file opened for WRITE.
Question 2
A OPENFILE "log.txt" FOR WRITE
B WRITEFILE "log.txt", "Done"
C CLOSEFILE "log.text"
Line C — The file identifier in CLOSEFILE must match the one used in OPENFILE. "log.text" is a different file, so log.txt is never closed. It should be CLOSEFILE "log.txt".
Question 3
A DECLARE Name : STRING
B OPENFILE "name.txt" FOR READ
C READFILE "names.txt", Name
D CLOSEFILE "names.txt"
Line B — The file name in OPENFILE (name.txt) does not match the one in READFILE and CLOSEFILE (names.txt). All file identifiers must be identical: OPENFILE "names.txt" FOR READ.
Question 4
A OPENFILE "marks.txt" FOR READ
B WRITEFILE "marks.txt", Mark
C CLOSEFILE "marks.txt"
Line A — WRITEFILE needs the file to be opened FOR WRITE. Opening it FOR READ is the wrong mode, so the data cannot be written. It should be OPENFILE "marks.txt" FOR WRITE.
Question 5
A DECLARE Pet : STRING
B // open the file here
C READFILE "pets.txt", Pet
D CLOSEFILE "pets.txt"
Line B — The OPENFILE line is missing, so READFILE tries to use a file that has not been opened. The line should be OPENFILE "pets.txt" FOR READ.

Student with a class code? Join your class →