← IGCSE Computer Science Paper 2

Declaring a variable to hold file data

Creates a variable named LineOfText that holds text. Declare it before the file commands that use it, for example before READFILE stores a line in it.

Understanding Declaring a variable to hold file data in IGCSE Computer Science Paper 2

What it does: The variable LineOfText exists before READFILE stores a line of text in it. PlayerName tells the reader what the variable holds, and STRING matches text data. In 0478 pseudocode the form is DECLARE <identifier> : <data type>, with a colon between the name and the data type. DECLARE only creates the variable. OPENFILE is the command that opens the file.

Common mistakes: The colon is missing. A declaration needs the form DECLARE <identifier> : <data type>, so it should be DECLARE LineOfText : STRING. A declaration uses a colon between the identifier and the data type, not an equals sign. It should be DECLARE LineOfText : STRING.

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
Which line correctly declares a variable to hold a line of text read from a file?
DECLARE LineOfText STRING =DECLARE LineOfText : STRING
Matches
READFILE "Notes.txt", LineOfText DECLARE LineOfText : STRING
↓
The variable is declared after READFILE uses it. DECLARE must come before the file command that uses the variable.
Applying
Meanings
Term
DECLARE LineOfText : STRING
Meaning
Creates a variable named LineOfText that holds text. Declare it before the file commands that use it, for example before READFILE stores a line in it.
Practice
DECLARE LineOfText : STRING
Testing
Gaps
LineOfText : STRING
Check Answer
Spots
1DECLARE LineOfText STRING
2OPENFILE "notes.txt" FOR READ

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

The 'Clownfish' badge — Reef Rookie, 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 correctly declares a variable to hold a line of text read from a file?
A. DECLARE LineOfText STRING =
B. DECLARE LineOfText : STRING ✓
In 0478 pseudocode the form is DECLARE <identifier> : <data type>, with a colon between the name and the data type.
2. Which line correctly declares a variable to hold a whole number, such as a score, that will be written to a file?
A. DECLARE Score : INTEGER ✓
B. DECLARE Score : STRING
A whole number is an INTEGER. STRING is for text, so it would be the wrong data type for a score.
3. When should DECLARE LineOfText : STRING appear in the program?
A. Before the READFILE command that stores a line in LineOfText ✓
B. After the READFILE command that stores a line in LineOfText
A variable must be declared before it is used. READFILE needs the variable to exist so it has somewhere to store the data.
4. Which identifier is the more meaningful choice for a variable that holds a line of text read from a file?
A. LineOfText ✓
B. x
A meaningful identifier shows what the variable holds, which makes the program easier to read and trace.
5. What does DECLARE do in a file handling program?
A. It opens the file so that data can be read from it
B. It creates a variable that can hold data read from or written to a file ✓
DECLARE only creates the variable. OPENFILE is the command that opens the file.

Match the Pairs

DECLARE LineOfText : STRING → Creates a variable called LineOfText that can hold a line of text read from or written to a file
DECLARE Score : INTEGER → Creates a variable called Score that can hold a whole number, such as a value read from a file
Declare the variable first, then use it to store a line read from the file → DECLARE LineOfText : STRING READFILE "Notes.txt", LineOfText
Give the variable a meaningful name and the correct data type for the file data → DECLARE PlayerName : STRING
READFILE "Notes.txt", LineOfText DECLARE LineOfText : STRING → The variable is declared after READFILE uses it. DECLARE must come before the file command that uses the variable.
DECLARE LineOfText STRING → The colon is missing between the identifier and the data type. It should be DECLARE LineOfText : STRING.

Key Term

DECLARE LineOfText : STRING
Creates a variable named LineOfText that holds text. Declare it before the file commands that use it, for example before READFILE stores a line in it.

Fill in the Blanks

1. DECLARE LineOfText : STRING
Hint: Type the keyword that creates a new variable before it is used.
2. DECLARE LineOfText : STRING
Hint: Type the data type used for variables that hold text.
3. DECLARE Score : INTEGER
Hint: Type the data type used for whole numbers.
4. DECLARE LineOfText : STRING OPENFILE "Notes.txt" FOR READ READFILE "Notes.txt", LineOfText
Hint: Type the name of the variable declared on the first line, which stores the line read from the file.
5. DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the variable name being declared, which must match the one written to the file on the last line.

Spot the Error

Question 1
A DECLARE LineOfText STRING
B OPENFILE "notes.txt" FOR READ
C READFILE "notes.txt", LineOfText
D CLOSEFILE "notes.txt"
Line A — The colon is missing. A declaration needs the form DECLARE <identifier> : <data type>, so it should be DECLARE LineOfText : STRING.
Question 2
A DECLARE Score : INTEGER
B DECLARE LineOfText = STRING
C OPENFILE "data.txt" FOR READ
D READFILE "data.txt", LineOfText
Line B — A declaration uses a colon between the identifier and the data type, not an equals sign. It should be DECLARE LineOfText : STRING.
Question 3
A DECLARE LineOfText : STRING
B OPENFILE "log.txt" FOR READ
C READFILE "log.txt", LineText
D CLOSEFILE "log.txt"
Line C — LineText was never declared. The variable that READFILE stores the data in must be the declared one, so it should be READFILE "log.txt", LineOfText.
Question 4
A DECLARE Score : INTEGER
B Score ← 50
C OPENFILE "score.txt" FOR WRTE
D WRITEFILE "score.txt", Score
E CLOSEFILE "score.txt"
Line C — WRTE is a spelling mistake. The file mode must be spelled WRITE, so it should be OPENFILE "score.txt" FOR WRITE.
Question 5
A DECLARE Name : STRING
B DECLARE Score : INTEGER
C Score ← "ten"
D Name ← "Ava"
Line C — Score was declared as INTEGER, so it cannot hold text. It should be assigned a whole number, such as Score ← 10.

Student with a class code? Join your class →