← IGCSE Computer Science Paper 2

READFILE: reading a line of text

Reads the next line of text from the open file Names.txt into the STRING variable LineOfText. Each READFILE call reads the following line. Declare the variable first.

Understanding READFILE: reading a line of text in IGCSE Computer Science Paper 2

What it does: A line of text is a string, so the variable that receives it must be declared as STRING before READFILE uses it. The file keeps track of its position, so the second READFILE reads the line after the one read by the first.

Common mistakes: Quotes make OUTPUT display the words TextLine instead of the line read from the file. It should be OUTPUT TextLine, with no quotes, so the variable's contents are displayed. READFILE needs the file opened in READ mode. WRITE mode would replace the file's data with an empty file. It should be OPENFILE "Names.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 statement reads one line of text from the open file Names.txt into a variable?
READFILE LineOfText, "Names.txt"READFILE "Names.txt", LineOfText
Matches
READFILE "Names.txt", "LineOfText"
↓
The variable name is in quotes, so it is a string literal and not a variable. It should be written without quotes: READFILE "Names.txt", LineOfText
Applying
Meanings
Term
READFILE "Names.txt", LineOfText
Meaning
Reads the next line of text from the open file Names.txt into the STRING variable LineOfText. Each READFILE call reads the following line. Declare the variable first.
Practice
DECLARE LineOfText : STRING
Testing
Gaps
"Names.txt", LineOfText
Check Answer
Spots
3READFILE "Names.txt", TextLine
4OUTPUT "TextLine"

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

The 'Viperfish' badge — Deep Debugger, 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 statement reads one line of text from the open file Names.txt into a variable?
A. READFILE "Names.txt", LineOfText ✓
B. READFILE LineOfText, "Names.txt"
The file identifier comes first, then a comma, then the variable that receives the line.
2. Which declaration should come before READFILE "Names.txt", LineOfText to store a line of text?
A. DECLARE LineOfText : STRING ✓
B. DECLARE LineOfText : INTEGER
A whole line of text is stored in a STRING variable. An INTEGER variable can only hold whole numbers.
3. Names.txt is open for READ. READFILE "Names.txt", LineOfText runs twice in a row. What happens?
A. Both calls read line 1 of the file
B. The first call reads line 1 and the second call reads line 2 ✓
Each READFILE call moves on and reads the next line of the file.
4. Names.txt contains the lines Ava, Liam, Mia. After one READFILE "Names.txt", LineOfText, what does LineOfText hold?
A. Ava ✓
B. Ava, Liam, Mia
One READFILE reads only one line, so LineOfText holds just the first line, Ava.
5. Which line shows the line of text that was just read on the screen?
A. READFILE LineOfText
B. OUTPUT LineOfText ✓
READFILE only stores the line in the variable. OUTPUT is used to display it.

Match the Pairs

READFILE "Names.txt", LineOfText → Reads the next line of text from the open file Names.txt and stores it in the variable LineOfText
OUTPUT LineOfText → Displays the line of text that READFILE has just stored in the variable
Declare the STRING variable first so it can hold a line of text read from the file → DECLARE LineOfText : STRING
Each READFILE call reads the next line, so two calls in a row read the first and then the second line → READFILE "Names.txt", LineOfText READFILE "Names.txt", LineOfText
READFILE "Names.txt", "LineOfText" → The variable name is in quotes, so it is a string literal and not a variable. It should be written without quotes: READFILE "Names.txt", LineOfText
DECLARE LineOfText : INTEGER READFILE "Names.txt", LineOfText → LineOfText is declared as INTEGER but a line of text must be stored in a STRING variable. It should be DECLARE LineOfText : STRING

Key Term

READFILE "Names.txt", LineOfText
Reads the next line of text from the open file Names.txt into the STRING variable LineOfText. Each READFILE call reads the following line. Declare the variable first.

Fill in the Blanks

1. READFILE "Names.txt", LineOfText
Hint: Type the keyword that reads the next line from an open file.
2. DECLARE LineOfText : STRING READFILE "Names.txt", LineOfText
Hint: Type the data type that holds a line of text.
3. READFILE "Names.txt", LineOfText OUTPUT LineOfText
Hint: Type the variable that receives the line read from the file.
4. READFILE "Names.txt", LineOfText OUTPUT LineOfText
Hint: Type the keyword that displays the line on the screen.
5. OPENFILE "Names.txt" FOR READ READFILE "Names.txt", LineOfText OUTPUT LineOfText
Hint: Type the file mode needed before lines can be read from the file.

Spot the Error

Question 1
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt", TextLine
D OUTPUT "TextLine"
E CLOSEFILE "Names.txt"
Line D — Quotes make OUTPUT display the words TextLine instead of the line read from the file. It should be OUTPUT TextLine, with no quotes, so the variable's contents are displayed.
Question 2
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR WRITE
C READFILE "Names.txt", TextLine
D OUTPUT TextLine
Line B — READFILE needs the file opened in READ mode. WRITE mode would replace the file's data with an empty file. It should be OPENFILE "Names.txt" FOR READ.
Question 3
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" READ
C READFILE "Names.txt", TextLine
D OUTPUT TextLine
E CLOSEFILE "Names.txt"
Line B — The keyword FOR is missing. The correct syntax is OPENFILE "Names.txt" FOR READ.
Question 4
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt" TextLine
D OUTPUT TextLine
E READFILE "Names.txt", TextLine
F OUTPUT TextLine
Line C — A comma is missing between the file name and the variable. It should be READFILE "Names.txt", TextLine.
Question 5
A DECLARE TextLine : INTEGER
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt", TextLine
D OUTPUT TextLine
Line A — A line of text must be stored in a STRING variable. An INTEGER variable cannot hold text. It should be DECLARE TextLine : STRING.

Student with a class code? Join your class →