← IGCSE Computer Science Paper 2

Why programs store data in a file

A file keeps data on storage so it still exists after the program closes, unlike variables. The program can read it back next time it runs, such as a saved high score or a list of names.

Understanding Why programs store data in a file in IGCSE Computer Science Paper 2

What it does: WRITEFILE puts the data into the file on storage, so the names survive after the program closes and can be read back later. READFILE gets data that was stored earlier, so the program can use a value saved on a previous run.

Common mistakes: The file is open for WRITE and the aim is to save the score so it survives after the program ends. The line should be WRITEFILE "score.txt", Score, not READFILE. READFILE needs a comma between the file name and the variable that will store the saved name. It should be READFILE "names.txt", Name.

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 479–488) 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
A program stores a player's score in a variable. The program ends. What happens to the score?
It is kept automatically, ready for the next time the program runsIt is lost, because variables only exist while the program runs
Matches
Score ← 250 // The program ends. Next run, the score is still 250 because variables keep their values.
↓
Variables do not keep their values after the program ends. The score must be saved in a file to last.
Applying
Meanings
Term
file
Meaning
A file keeps data on storage so it still exists after the program closes, unlike variables. The program can read it back next time it runs, such as a saved high score or a list of names.
Practice
DECLARE HighScore : INTEGER
Testing
Gaps
OPENFILE "HighScore.txt" FOR
Check Answer
Spots
3OPENFILE "score.txt" FOR WRITE
4READFILE "score.txt", Score

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

The 'Constellation Wolf' badge — Space Legend, 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 stores a player's score in a variable. The program ends. What happens to the score?
A. It is lost, because variables only exist while the program runs ✓
B. It is kept automatically, ready for the next time the program runs
Variables are held in memory while the program runs. When the program ends, their values are lost. A file is needed to keep the data.
2. Which is the best way to keep a high score so it is still there tomorrow?
A. Store the score in a variable
B. Write the score to a file ✓
A file keeps data on storage, so it survives after the program closes. A variable is lost when the program ends.
3. Which statement about file data is correct?
A. It still exists after the program closes ✓
B. It disappears as soon as the program closes
Data in a file is kept on storage, so it survives after the program has finished running.
4. A quiz program runs a second time. How can it get the high score saved last time?
A. Read the score back from the file ✓
B. Read the score from the old variable
The variable was lost when the first run ended. The score was kept in the file, so the program reads it back from there.
5. A teacher wants a program to remember a list of student names between lessons. What should the program use?
A. A variable that holds the names
B. A file that stores the names ✓
A file keeps the list on storage between runs. A variable would be empty again when the program next starts.

Match the Pairs

OPENFILE "HighScore.txt" FOR WRITE → Opens a file on storage so the program can save its data there, and the data stays after the program ends
score ← 250 → Stores a value in a variable, which is lost when the program ends
A program saves a list of names to a file so they are still there the next time it runs → WRITEFILE "Names.txt", Name
A program reads the saved high score back from storage when it starts again → READFILE "HighScore.txt", Score
Score ← 250 // The program ends. Next run, the score is still 250 because variables keep their values. → Variables do not keep their values after the program ends. The score must be saved in a file to last.
// A file is deleted from storage when the program closes, so the data is gone. → A file is not deleted when the program closes. It stays on storage, so the data can be read again later.

Key Term

file
A file keeps data on storage so it still exists after the program closes, unlike variables. The program can read it back next time it runs, such as a saved high score or a list of names.

Fill in the Blanks

1. OPENFILE "HighScore.txt" FOR WRITE
Hint: Type the file mode used to save data into a file.
2. OPENFILE "HighScore.txt" FOR WRITE WRITEFILE "HighScore.txt", Score CLOSEFILE "HighScore.txt"
Hint: Type the command that stores a value in the open file so it survives after the program ends.
3. OPENFILE "HighScore.txt" FOR READ READFILE "HighScore.txt", Score CLOSEFILE "HighScore.txt"
Hint: Type the variable that receives the saved high score when the program runs again.
4. OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
Hint: Type the command that finishes with the file after the list of names is saved.
5. // Next run: get the saved names back OPENFILE "Names.txt" FOR READ
Hint: Type the file mode used to load saved data back into the program next time it runs.

Spot the Error

Question 1
A DECLARE Score : INTEGER
B Score ← 250
C OPENFILE "score.txt" FOR WRITE
D READFILE "score.txt", Score
E CLOSEFILE "score.txt"
Line D — The file is open for WRITE and the aim is to save the score so it survives after the program ends. The line should be WRITEFILE "score.txt", Score, not READFILE.
Question 2
A OPENFILE "names.txt" FOR READ
B READFILE "names.txt" Name
C OUTPUT Name
D CLOSEFILE "names.txt"
Line B — READFILE needs a comma between the file name and the variable that will store the saved name. It should be READFILE "names.txt", Name.
Question 3
A OPENFILE "best.txt" FOR WRITE
B WRITEFILE best.txt, Best
C CLOSEFILE "best.txt"
Line B — The file name is a string, so it must be in quotes. Without them the program cannot find the file that stores the high score. It should be WRITEFILE "best.txt", Best.
Question 4
A OPENFILE "best.txt" FOR WRITE
B READFILE "best.txt", Best
C OUTPUT "High score: ", Best
D CLOSEFILE "best.txt"
Line A — To read the saved high score back next time the program runs, the file must be opened FOR READ. WRITE mode would replace the old data. It should be OPENFILE "best.txt" FOR READ.
Question 5
A DECLARE Names : STRING
B OPENFILE "names.txt" FOR READ
C READFILE Names, "names.txt"
D OUTPUT Names
Line C — The file name must come first and the variable second. It should be READFILE "names.txt", Names so the saved data is read into the variable.

Student with a class code? Join your class →