← IGCSE Computer Science Paper 2

Write, close, then read the data back

Open a file for WRITE, store data with WRITEFILE, then CLOSEFILE. Reopen the same file name for READ and READFILE gets the data back to use again.

Understanding Write, close, then read the data back in IGCSE Computer Science Paper 2

What it does: This is the saving stage: OPENFILE for WRITE, WRITEFILE to store the data, then CLOSEFILE to finish. READFILE puts the stored data into the variable Saved, and OUTPUT displays it to prove the data was kept. The file must be closed after writing. Then it can be reopened in READ mode so the saved data can be read back.

Common mistakes: Line 2 must store the data in the file, so it needs WRITEFILE, not READFILE. It should be WRITEFILE "data.txt", Score. The program writes to the file next, so it must be opened for WRITE first. It should be OPENFILE "name.txt" FOR WRITE.

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
A program has written a value to "scores.txt" using WRITEFILE. What must happen before it reopens the file for READ?
CLOSEFILE "scores.txt"OUTPUT "scores.txt"
Matches
OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score OPENFILE "Data.txt" FOR READ
↓
CLOSEFILE is missing. The file must be closed after writing before it is reopened in READ mode.
Applying
Meanings
Term
WRITE, CLOSEFILE, READ
Meaning
Open a file for WRITE, store data with WRITEFILE, then CLOSEFILE. Reopen the same file name for READ and READFILE gets the data back to use again.
Practice
OPENFILE "Name.txt" FOR WRITE
Testing
Gaps
"score.txt"
Check Answer
Spots
1OPENFILE "data.txt" FOR WRITE
2READFILE "data.txt", Score

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

The 'Blobfish' badge — Pressure Handler, 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 has written a value to "scores.txt" using WRITEFILE. What must happen before it reopens the file for READ?
A. OUTPUT "scores.txt"
B. CLOSEFILE "scores.txt" ✓
The file must be closed after writing. Then it can be reopened in READ mode so the saved data can be read back.
2. Which order saves data and then uses it again?
A. OPENFILE FOR WRITE, WRITEFILE, CLOSEFILE, OPENFILE FOR READ, READFILE ✓
B. OPENFILE FOR READ, READFILE, OPENFILE FOR WRITE, WRITEFILE, CLOSEFILE
Data must be written and the file closed before the file is opened for READ. Reading first means there is nothing saved to read yet.
3. Which pair of statements uses the same file correctly in both modes?
A. OPENFILE "pets.txt" FOR WRITE ... OPENFILE "pets.txt" FOR READ ✓
B. OPENFILE "pets.txt" FOR WRITE ... OPENFILE "animals.txt" FOR READ
The file identifier must be identical in both OPENFILE statements. A different name opens a different file, so the saved data is not found.
4. A program runs WRITEFILE "Mia" in WRITE mode, then CLOSEFILE, then reopens the same file for READ and runs READFILE Name. What does Name hold?
A. Nothing, because the data is lost when the file is closed
B. "Mia" ✓
Closing a file does not delete the data. It is kept in the file, and READFILE gets it back.
5. What is the purpose of OUTPUT Name after READFILE Name?
A. It shows the data that was kept in the file ✓
B. It saves the data into the file
OUTPUT displays a value on the screen. Saving to a file is done with WRITEFILE, so OUTPUT is used to show the data was read back.

Match the Pairs

CLOSEFILE("Scores.txt") → Finishes writing and saves the file, so it can be reopened in READ mode to get the data back
OPENFILE "Scores.txt" FOR READ → Reopens the file that was written earlier, using the same file name, so its data can be read back
Data is saved by opening the file for WRITE, writing the item, then closing the file → OPENFILE "Name.txt" FOR WRITE WRITEFILE "Name.txt", Name CLOSEFILE("Name.txt")
After reopening the file for READ, READFILE gets back the stored data and OUTPUT shows it was kept → OPENFILE "Name.txt" FOR READ READFILE "Name.txt", Saved OUTPUT Saved
OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score OPENFILE "Data.txt" FOR READ → CLOSEFILE is missing. The file must be closed after writing before it is reopened in READ mode.
OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score CLOSEFILE("Data.txt") OPENFILE "Score.txt" FOR READ → The file name in the READ statement is different from the one written, so the saved data is not found.

Key Term

WRITE, CLOSEFILE, READ
Open a file for WRITE, store data with WRITEFILE, then CLOSEFILE. Reopen the same file name for READ and READFILE gets the data back to use again.

Fill in the Blanks

1. OPENFILE "score.txt" FOR WRITE WRITEFILE "score.txt", Points CLOSEFILE "score.txt"
Hint: Type the command that finishes with the file after writing, before it is reopened in the other mode.
2. CLOSEFILE "names.txt" OPENFILE "names.txt" FOR READ READFILE "names.txt", Name
Hint: Type the file name that must match the one used when the data was written. Look at the line above.
3. OPENFILE "age.txt" FOR READ READFILE "age.txt", Age OUTPUT Age
Hint: Type the command that gets the stored data back from the file into the variable.
4. CLOSEFILE "age.txt" OPENFILE "age.txt" FOR READ READFILE "age.txt", Age
Hint: Type the file mode needed to get the saved data back out of the file.
5. OPENFILE "age.txt" FOR READ READFILE "age.txt", Age OUTPUT Age
Hint: Type the keyword that displays the value on screen to show the data was kept.

Spot the Error

Question 1
A OPENFILE "data.txt" FOR WRITE
B READFILE "data.txt", Score
C CLOSEFILE "data.txt"
D OPENFILE "data.txt" FOR READ
E READFILE "data.txt", Back
F OUTPUT Back
Line B — Line 2 must store the data in the file, so it needs WRITEFILE, not READFILE. It should be WRITEFILE "data.txt", Score.
Question 2
A OPENFILE "name.txt" FOR READ
B WRITEFILE "name.txt", Name
C CLOSEFILE "name.txt"
D OPENFILE "name.txt" FOR READ
E READFILE "name.txt", Saved
F OUTPUT Saved
Line A — The program writes to the file next, so it must be opened for WRITE first. It should be OPENFILE "name.txt" FOR WRITE.
Question 3
A OPENFILE "mark.txt" FOR WRITE
B WRITEFILE "mark.txt", Mark
C CLOSEFILE "mark.txt"
D OPENFILE "marks.txt" FOR READ
E READFILE "mark.txt", Result
F OUTPUT Result
Line D — The file identifier must be the same in both OPENFILE statements. "marks.txt" is a different file, so it should be "mark.txt".
Question 4
A OPENFILE "city.txt" FOR WRITE
B WRITEFILE City, "city.txt"
C CLOSEFILE "city.txt"
D OPENFILE "city.txt" FOR READ
E READFILE "city.txt", Town
F OUTPUT Town
Line B — WRITEFILE needs the file identifier first, then the data to write. It should be WRITEFILE "city.txt", City.
Question 5
A OPENFILE "age.txt" FOR WRITE
B WRITEFILE "age.txt", Age
C CLOSEFILE "age.txt"
D OPENFILE "age.txt" READ
E READFILE "age.txt", NewAge
F OUTPUT NewAge
Line D — The keyword FOR is missing before the mode. It should be OPENFILE "age.txt" FOR READ.

Student with a class code? Join your class →