← IGCSE Computer Science Paper 2

READFILE: reading a single item of data

Reads one data item from the open file and stores it in the variable. The file must already be open in READ mode, and the variable's data type must match the item.

Understanding READFILE: reading a single item of data in IGCSE Computer Science Paper 2

What it does: The variable PlayerName, written after the comma, receives the item that READFILE reads from Names.txt. Declaring Score as INTEGER means it can hold a whole-number item read from the file, so the data types match. READFILE only works on a file that is already open in READ mode.

Common mistakes: READFILE only works on a file that is open in READ mode. The file was opened FOR WRITE, so line 2 should end with FOR READ. READFILE needs a comma between the file identifier and the variable: READFILE "Scores.txt", Score.

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 reads one item from Scores.txt into the variable Score?
READFILE "Scores.txt", ScoreREADFILE Score, "Scores.txt"
Matches
READFILE Score, "Scores.txt"
↓
The arguments are in the wrong order. The file identifier must come first, then the variable.
Applying
Meanings
Term
READFILE "Scores.txt", Score
Meaning
Reads one data item from the open file and stores it in the variable. The file must already be open in READ mode, and the variable's data type must match the item.
Practice
DECLARE Score : INTEGER
Testing
Gaps
"Scores.txt", Score
Check Answer
Spots
1DECLARE Score : INTEGER
2OPENFILE "Marks.txt" FOR WRITE

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

The 'Chambered Nautilus' badge — File Explorer, 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 reads one item from Scores.txt into the variable Score?
A. READFILE "Scores.txt", Score ✓
B. READFILE Score, "Scores.txt"
The file identifier comes first, then a comma, then the variable that will store the item.
2. Which statement must run before READFILE "Scores.txt", Score?
A. OPENFILE "Scores.txt" FOR WRITE
B. OPENFILE "Scores.txt" FOR READ ✓
READFILE only works on a file that is already open in READ mode.
3. After READFILE "Scores.txt", Score runs, where is the data item stored?
A. In the variable Score ✓
B. In the file identifier "Scores.txt"
READFILE assigns the item it reads to the variable named after the comma.
4. Scores.txt holds whole numbers such as 42. Which declaration suits Score?
A. DECLARE Score : BOOLEAN
B. DECLARE Score : INTEGER ✓
The variable's data type must match the type of the item in the file. Whole numbers need INTEGER.
5. What does one READFILE "Scores.txt", Score statement do?
A. Reads one data item from the open file ✓
B. Reads every item in the file at once
Each READFILE reads a single item. To read more items you need to run it again, for example in a loop.

Match the Pairs

READFILE "Scores.txt", Score → Reads one data item from the open file Scores.txt and stores it in the variable Score
OPENFILE "Scores.txt" FOR READ → Must be done before READFILE so the file is open in READ mode and ready to be read
The item read from the file is stored in the variable named after the file identifier → READFILE "Names.txt", PlayerName
The variable's data type must match the item being read, so an integer score needs an INTEGER variable → DECLARE Score : INTEGER READFILE "Scores.txt", Score
READFILE Score, "Scores.txt" → The arguments are in the wrong order. The file identifier must come first, then the variable.
READFILE "Scores.txt", Score CLOSEFILE "Scores.txt" → There is no OPENFILE ... FOR READ before READFILE, so the file is not open and cannot be read.

Key Term

READFILE "Scores.txt", Score
Reads one data item from the open file and stores it in the variable. The file must already be open in READ mode, and the variable's data type must match the item.

Fill in the Blanks

1. DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the keyword that reads one data item from the open file.
2. OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the file mode that must be used before reading from a file.
3. DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the data type that matches a whole-number item in the file.
4. OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Hint: Type the keyword that opens the file so it is ready to be read.
5. OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score OUTPUT Score
Hint: Type the variable that now holds the item read from the file.

Spot the Error

Question 1
A DECLARE Score : INTEGER
B OPENFILE "Marks.txt" FOR WRITE
C READFILE "Marks.txt", Score
D OUTPUT Score
Line B — READFILE only works on a file that is open in READ mode. The file was opened FOR WRITE, so line 2 should end with FOR READ.
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" FOR READ
C READFILE "Scores.txt" Score
D CLOSEFILE "Scores.txt"
Line C — READFILE needs a comma between the file identifier and the variable: READFILE "Scores.txt", Score.
Question 3
A DECLARE Score : STRING
B OPENFILE "Scores.txt" FOR READ
C READFILE "Scores.txt", Score
D OUTPUT Score + 10
Line A — The file item is a whole number and the program adds 10 to it. The variable's data type must match the item, so Score should be declared as INTEGER, not STRING.
Question 4
A DECLARE Name : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE Name, "Names.txt"
D OUTPUT Name
Line C — The arguments are the wrong way round. The file identifier comes first and the variable second: READFILE "Names.txt", Name.
Question 5
A DECLARE Mark : INTEGER
B OPENFILE "Marks.txt" READ
C READFILE "Marks.txt", Mark
D OUTPUT Mark
Line B — The keyword FOR is missing. The correct line is OPENFILE "Marks.txt" FOR READ, so the file is open in READ mode before READFILE.

Student with a class code? Join your class →