← IGCSE Computer Science Paper 2

CLOSEFILE

Closes the file named in the identifier once you have finished reading or writing, so data is saved and the file is released. The name must match the one used in OPENFILE.

Understanding CLOSEFILE in IGCSE Computer Science Paper 2

What it does: All three statements use "Names.txt", so CLOSEFILE closes the file that OPENFILE opened. Two files were opened, so each one gets its own CLOSEFILE with its own identifier. The 0478 keyword is CLOSEFILE, followed by the file identifier. CLOSE on its own is not a pseudocode keyword. CLOSEFILE comes after the last WRITEFILE. Once a file is closed you cannot write to it unless you open it again. Closing makes sure written data is stored safely and frees the file for other uses. It does not delete anything. The file identifier is a string, so it needs quotation marks.

Common mistakes: The identifier in CLOSEFILE must match the one used in OPENFILE. "Date.txt" is a different file name, so it should be CLOSEFILE "Data.txt". The file identifier is a string, so it needs quote marks. It should be CLOSEFILE "Data.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
Which line correctly closes a file called Scores.txt in 0478 pseudocode?
CLOSE "Scores.txt"CLOSEFILE "Scores.txt"
Matches
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Score.txt"
↓
The identifier in CLOSEFILE ("Score.txt") does not match the one in OPENFILE ("Scores.txt"), so the opened file is never closed. It should be CLOSEFILE "Scores.txt".
Applying
Meanings
Term
CLOSEFILE "Scores.txt"
Meaning
Closes the file named in the identifier once you have finished reading or writing, so data is saved and the file is released. The name must match the one used in OPENFILE.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
"Scores.txt"
Check Answer
Spots
2WRITEFILE "Data.txt", Score
3CLOSEFILE "Date.txt"

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

The 'Penguin' badge — List Waddler, 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 line correctly closes a file called Scores.txt in 0478 pseudocode?
A. CLOSEFILE "Scores.txt" ✓
B. CLOSE "Scores.txt"
The 0478 keyword is CLOSEFILE, followed by the file identifier. CLOSE on its own is not a pseudocode keyword.
2. A program ran OPENFILE "Scores.txt" FOR WRITE. Which line closes it?
A. CLOSEFILE "Scores.txt" ✓
B. CLOSEFILE "Marks.txt"
The identifier in CLOSEFILE must match the one used in OPENFILE, so the same file is closed.
3. When should CLOSEFILE be used?
A. After the last read or write on the file ✓
B. Before the file has been opened
A file is closed once you have finished with it. You cannot close a file that has not been opened.
4. A program opens two files, Input.txt and Output.txt. What should it do?
A. Close only the first file it opened
B. Close both files with CLOSEFILE ✓
Every file that was opened should be closed, so each one needs its own CLOSEFILE.
5. Which is the correct order of statements?
A. OPENFILE, CLOSEFILE, WRITEFILE
B. OPENFILE, WRITEFILE, CLOSEFILE ✓
CLOSEFILE comes after the last WRITEFILE. Once a file is closed you cannot write to it unless you open it again.

Match the Pairs

CLOSEFILE "Scores.txt" → Closes the file named Scores.txt when the program has finished with it, so the data is saved and the file is released.
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" → The file is closed straight after the last write, and the identifier is the same in all three statements.
The identifier in CLOSEFILE must match the identifier used in OPENFILE → OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Every file that was opened must be closed, so two opened files need two CLOSEFILE statements → OPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE "In.txt" CLOSEFILE "Out.txt"
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Score.txt" → The identifier in CLOSEFILE ("Score.txt") does not match the one in OPENFILE ("Scores.txt"), so the opened file is never closed. It should be CLOSEFILE "Scores.txt".
OPENFILE "Scores.txt" FOR READ CLOSEFILE "Scores.txt" READFILE "Scores.txt", Score → CLOSEFILE is placed before the READFILE, so the file is closed before the data is read. It should come after the last read.

Key Term

CLOSEFILE "Scores.txt"
Closes the file named in the identifier once you have finished reading or writing, so data is saved and the file is released. The name must match the one used in OPENFILE.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Hint: Type the keyword that closes a file once you have finished writing to it.
2. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Hint: Type the file name inside the quotes. It must match the one used in OPENFILE.
3. OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Hint: Type the keyword that releases the file after the last read.
4. OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Hint: Type the file name inside the quotes. It must be the same as the one used in OPENFILE.
5. READFILE "Scores.txt", Mark OUTPUT Mark CLOSEFILE "Scores.txt"
Hint: Type the keyword used to close a file after the final read is done.

Spot the Error

Question 1
A OPENFILE "Data.txt" FOR WRITE
B WRITEFILE "Data.txt", Score
C CLOSEFILE "Date.txt"
Line C — The identifier in CLOSEFILE must match the one used in OPENFILE. "Date.txt" is a different file name, so it should be CLOSEFILE "Data.txt".
Question 2
A OPENFILE "Data.txt" FOR READ
B READFILE "Data.txt", Line
C OUTPUT Line
D CLOSEFILE Data.txt
Line D — The file identifier is a string, so it needs quote marks. It should be CLOSEFILE "Data.txt".
Question 3
A OPENFILE "Mark.txt" FOR WRITE
B WRITEFILE "Marks.txt", Mark
C CLOSEFILE "Marks.txt"
Line A — The file opened is "Mark.txt", but the file written to and closed is "Marks.txt". The identifier must be the same in every statement: OPENFILE "Marks.txt" FOR WRITE.
Question 4
A OPENFILE "Data.txt" FOR READ
B READFILE "Data.txt", Line
C CLOSEFILE "Data.txt", Line
D OUTPUT Line
Line C — CLOSEFILE takes only the file identifier. It does not take a variable. It should be CLOSEFILE "Data.txt".
Question 5
A OPENFILE "Data.txt" FOR WRITE
B WRITEFILE "Data.txt", Name
C WRITEFILE "Data.txt", Score
D CLOSE "Data.txt"
Line D — The keyword is CLOSEFILE, not CLOSE. After the last write the program should use CLOSEFILE "Data.txt".

Student with a class code? Join your class →