← IGCSE Computer Science Paper 2

OPENFILE ... FOR READ

Opens the existing file Scores.txt in READ mode so the program can read data from it with READFILE. The file must already exist, and READ mode does not change its contents.

Understanding OPENFILE ... FOR READ in IGCSE Computer Science Paper 2

What it does: OPENFILE ... FOR READ comes first, and only after that can READFILE take data from the file into the variable Score. The file identifier is the name of the file, written as a string in quote marks.

Common mistakes: The program wants to read existing data, so the file must be opened in READ mode. FOR WRITE would replace the contents. It should be OPENFILE "Scores.txt" FOR READ. The keyword FOR is missing before the file mode. It should be OPENFILE "Scores.txt" FOR READ.

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 opens the existing file Scores.txt so a program can read from it?
OPENFILE "Scores.txt" FOR READOPENFILE "Scores.txt" FOR WRITE
Matches
OPENFILE Scores.txt FOR READ
↓
The file name is not in quotation marks. The file identifier must be a string, so it should be "Scores.txt".
Applying
Meanings
Term
OPENFILE "Scores.txt" FOR READ
Meaning
Opens the existing file Scores.txt in READ mode so the program can read data from it with READFILE. The file must already exist, and READ mode does not change its contents.
Practice
DECLARE Score : INTEGER
Testing
Gaps
"Scores.txt" FOR READ
Check Answer
Spots
1OPENFILE "Scores.txt" FOR WRITE
2READFILE "Scores.txt", Score

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

The 'Deer' badge — Forest Streak, 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 opens the existing file Scores.txt so a program can read from it?
A. OPENFILE "Scores.txt" FOR READ ✓
B. OPENFILE "Scores.txt" FOR WRITE
FOR READ opens an existing file so data can be read from it. FOR WRITE is used to put data into a file.
2. True or false: A file must already exist before you can use OPENFILE ... FOR READ on it.
A. False
B. True ✓
READ mode does not create a file. The file must already exist and should hold the data you want to read.
3. Which order is correct for reading a line from a file?
A. OPENFILE "Scores.txt" FOR READ, then READFILE "Scores.txt", Line ✓
B. READFILE "Scores.txt", Line, then OPENFILE "Scores.txt" FOR READ
You must open the file before you use READFILE on it. Reading from a file that is not open is an error.
4. What happens to the data stored in Scores.txt when you run OPENFILE "Scores.txt" FOR READ?
A. The old data is erased
B. The data stays exactly the same ✓
READ mode does not change the file. Only WRITE mode replaces old data with new data.
5. Which statement is correct about the file identifier in OPENFILE "Scores.txt" FOR READ?
A. "Scores.txt" is the file name, written as a string ✓
B. Scores.txt is a variable, so it has no quote marks
The file identifier is the name of the file, written as a string in quote marks.

Match the Pairs

OPENFILE "Scores.txt" FOR READ → Opens the existing file Scores.txt so the program can read data from it
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name → Opens Names.txt first, then reads one line from it into the variable Name
A file must be opened in READ mode before any data is read from it → OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
READ mode opens a file that already exists and holds data, so it is not used to create a new file → OPENFILE "Results.txt" FOR READ
OPENFILE Scores.txt FOR READ → The file name is not in quotation marks. The file identifier must be a string, so it should be "Scores.txt".
READFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR READ → READFILE is used before the file is opened. OPENFILE ... FOR READ must come first.

Key Term

OPENFILE "Scores.txt" FOR READ
Opens the existing file Scores.txt in READ mode so the program can read data from it with READFILE. The file must already exist, and READ mode does not change its contents.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR READ
Hint: Type the command that opens a file before the program uses it.
2. OPENFILE "Scores.txt" FOR READ
Hint: Type the keyword that comes between the file identifier and the file mode.
3. OPENFILE "Scores.txt" FOR READ
Hint: Type the file mode used to read data from a file that already exists.
4. OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the command that takes data from the file once it has been opened.
5. DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the name of the variable that was declared to hold the data read from the file.

Spot the Error

Question 1
A OPENFILE "Scores.txt" FOR WRITE
B READFILE "Scores.txt", Score
C OUTPUT Score
D CLOSEFILE "Scores.txt"
Line A — The program wants to read existing data, so the file must be opened in READ mode. FOR WRITE would replace the contents. It should be OPENFILE "Scores.txt" FOR READ.
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" READ
C READFILE "Scores.txt", Score
D OUTPUT Score
E CLOSEFILE "Scores.txt"
Line B — The keyword FOR is missing before the file mode. It should be OPENFILE "Scores.txt" FOR READ.
Question 3
A DECLARE Mark : INTEGER
B OUTPUT "Reading marks"
C OPENFILE "Marks.txt" FOR READ
D READFILE "Marks.txt" Mark
E OUTPUT Mark
Line D — READFILE needs a comma between the file identifier and the variable. It should be READFILE "Marks.txt", Mark.
Question 4
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" FOR READ
C WRITEFILE "Scores.txt", Score
D OUTPUT Score
Line C — The file was opened FOR READ, so the program must use READFILE to get data from it. WRITEFILE is for a file opened in WRITE mode. It should be READFILE "Scores.txt", Score.
Question 5
A DECLARE Total : INTEGER
B OPENFILE Scores.txt FOR READ
C READFILE "Scores.txt", Total
D OUTPUT Total
Line B — The file identifier is a string, so it needs quotation marks. It should be OPENFILE "Scores.txt" FOR READ.

Student with a class code? Join your class →