← IGCSE Computer Science Paper 2

Why a file must be closed

Closes an open file so its data is safely saved and the file can be opened again in a different mode, such as READ after WRITE. Use one CLOSEFILE for each OPENFILE.

Understanding Why a file must be closed in IGCSE Computer Science Paper 2

What it does: Each file opened gets its own CLOSEFILE, one for In.txt and one for Out.txt. The CLOSEFILE between the WRITEFILE and the OPENFILE FOR READ ends WRITE mode so the file can be reopened in READ mode. An unclosed file may not have all its data saved properly, so later reading may fail or give missing data.

Common mistakes: The program writes to the file on line 2, so it must be opened in WRITE mode. A file open for READ cannot be written to. Line 1 should be OPENFILE "mark.txt" FOR WRITE. CLOSEFILE must use the same file name as the OPENFILE it matches. Here log.txt is never closed, so its data may not be saved and it cannot safely be opened in READ mode. 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 489–499) and word search (Modules 479–499).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Why do we use CLOSEFILE when we have finished with a file?
So the file is deleted from the computerSo the data is safely saved and the file can be opened again
Matches
OPENFILE "Scores.txt" FOR WRITE WRITEFILE("Scores.txt", Score) OPENFILE "Scores.txt" FOR READ
↓
CLOSEFILE is missing after writing. The file is still open in WRITE mode, so it cannot be opened for READ and the data may not be saved. Add CLOSEFILE("Scores.txt") before the second OPENFILE.
Applying
Meanings
Term
CLOSEFILE
Meaning
Closes an open file so its data is safely saved and the file can be opened again in a different mode, such as READ after WRITE. Use one CLOSEFILE for each OPENFILE.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
"Scores.txt"
Check Answer
Spots
1OPENFILE "mark.txt" FOR READ
2WRITEFILE "mark.txt", Score

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

The 'Angelfish' badge — Reef Perfectionist, 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. Why do we use CLOSEFILE when we have finished with a file?
A. So the data is safely saved and the file can be opened again ✓
B. So the file is deleted from the computer
CLOSEFILE finishes the work on the file. It does not delete anything. It makes sure the data is saved and frees the file to be opened again.
2. A program writes names to a file, then wants to read them back. What must happen in between?
A. OPENFILE "Names.txt" FOR READ straight away, with no CLOSEFILE
B. CLOSEFILE "Names.txt" then OPENFILE "Names.txt" FOR READ ✓
The file is open in WRITE mode. It must be closed before it can be opened again in READ mode.
3. Which statement about file modes is correct?
A. A file is open in only one mode at a time ✓
B. A file can be open in READ and WRITE mode at the same time
In 0478 pseudocode a file is open for READ or for WRITE, never both at once. Close it to change mode.
4. Which program part is correct?
A. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score (no CLOSEFILE at the end)
B. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" ✓
Forgetting CLOSEFILE is a common mistake. Every OPENFILE needs a matching CLOSEFILE once the program has finished with the file.
5. A program opens two files, InFile.txt and OutFile.txt. How many CLOSEFILE statements are needed?
A. Two: one CLOSEFILE for each OPENFILE ✓
B. One: a single CLOSEFILE closes every file
Each CLOSEFILE closes one named file. Use one CLOSEFILE for each OPENFILE.

Match the Pairs

CLOSEFILE("Scores.txt") → Closes the open file so its data is safely saved and it is free to be opened again in a different mode
OPENFILE "Scores.txt" FOR WRITE CLOSEFILE("Scores.txt") OPENFILE "Scores.txt" FOR READ → The file is closed after WRITE so it can then be opened in READ mode
Every OPENFILE needs its own matching CLOSEFILE, so two open files need two CLOSEFILE statements → OPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE("In.txt") CLOSEFILE("Out.txt")
A file is open in only one mode at a time, so close it after writing before reading the data back → WRITEFILE("Names.txt", Name) CLOSEFILE("Names.txt") OPENFILE "Names.txt" FOR READ
OPENFILE "Scores.txt" FOR WRITE WRITEFILE("Scores.txt", Score) OPENFILE "Scores.txt" FOR READ → CLOSEFILE is missing after writing. The file is still open in WRITE mode, so it cannot be opened for READ and the data may not be saved. Add CLOSEFILE("Scores.txt") before the second OPENFILE.
OPENFILE "A.txt" FOR READ OPENFILE "B.txt" FOR WRITE CLOSEFILE("B.txt") → Only one CLOSEFILE is used for two OPENFILE statements. A.txt is never closed. Add CLOSEFILE("A.txt") so there is one CLOSEFILE for each OPENFILE.

Key Term

CLOSEFILE
Closes an open file so its data is safely saved and the file can be opened again in a different mode, such as READ after WRITE. Use one CLOSEFILE for each OPENFILE.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Hint: Type the command that finishes with the file so the written data is safely saved.
2. CLOSEFILE "Scores.txt" OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the file mode used to get data back out of a file that was closed after writing.
3. OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
Hint: Type the keyword that opens a file. Every use of it needs one matching close command.
4. OPENFILE "Log.txt" FOR WRITE WRITEFILE "Log.txt", Entry CLOSEFILE "Log.txt"
Hint: Type the mode used to put data into a file. The file must be closed before it can be opened in another mode.
5. OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Hint: Type the command needed when you finish with a file you opened, even one only used for reading.

Spot the Error

Question 1
A OPENFILE "mark.txt" FOR READ
B WRITEFILE "mark.txt", Score
C CLOSEFILE "mark.txt"
Line A — The program writes to the file on line 2, so it must be opened in WRITE mode. A file open for READ cannot be written to. Line 1 should be OPENFILE "mark.txt" FOR WRITE.
Question 2
A OPENFILE "log.txt" FOR WRITE
B WRITEFILE "log.txt", Msg
C CLOSEFILE "notes.txt"
D OPENFILE "log.txt" FOR READ
Line C — CLOSEFILE must use the same file name as the OPENFILE it matches. Here log.txt is never closed, so its data may not be saved and it cannot safely be opened in READ mode. It should be CLOSEFILE "log.txt".
Question 3
A DECLARE Name : STRING
B OPENFILE "names.txt" FOR READ
C WRITEFILE "names.txt", Name
D CLOSEFILE "names.txt"
Line C — The file was opened in READ mode, and a file is open in only one mode at a time. WRITEFILE cannot be used in READ mode. Line 3 should be READFILE "names.txt", Name.
Question 4
A OPENFILE "a.txt" FOR WRITE
B WRITEFILE "a.txt", Data
C CLOSEFILE "a.txt"
D OPENFILE "a.txt" FOR WRITE
E READFILE "a.txt", Data
F CLOSEFILE "a.txt"
Line D — After closing the file, the program wants to read the saved data back. Opening in WRITE mode would replace the data with an empty file. Line 4 should be OPENFILE "a.txt" FOR READ.
Question 5
A OPEN "data.txt" FOR WRITE
B WRITEFILE "data.txt", Total
C CLOSEFILE "data.txt"
Line A — The keyword to open a file in 0478 pseudocode is OPENFILE, not OPEN. Line 1 should be OPENFILE "data.txt" FOR WRITE.

Student with a class code? Join your class →