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".
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.
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
CLOSEFILEFill in the Blanks
Spot the Error
Student with a class code? Join your class →