← IGCSE Computer Science Paper 2

Matching the variable's data type to the file data

The variable used with READFILE or WRITEFILE needs a data type that suits the data: STRING for a line of text, INTEGER for a whole number. A wrong type causes errors.

Understanding Matching the variable's data type to the file data in IGCSE Computer Science Paper 2

What it does: The variable is declared as STRING first, so it matches the text that READFILE reads. Total is declared as INTEGER, which suits the whole number that WRITEFILE stores. The data type must suit the data being read, otherwise errors can occur. "Hello class" is text, so STRING is needed. An INTEGER can only store whole numbers. The type is chosen in the DECLARE statement, before the variable is used with the file.

Common mistakes: Total is an INTEGER read from the file, so you can only add a whole number to it. "10" is a string. It should be Total ← Total + 10. Score is declared as INTEGER, so it must hold a whole number. A string cannot be stored in it. It should be Score ← 85.

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 declaration suits a variable that will hold a line of text read from a file?
DECLARE LineOfText : INTEGERDECLARE LineOfText : STRING
Matches
DECLARE Age : STRING READFILE "ages.txt", Age Total ← Age + 1
↓
Age holds a whole number but is declared as STRING, so using it in arithmetic causes an error. It should be declared as INTEGER.
Applying
Meanings
Term
DECLARE LineOfText : STRING
Meaning
The variable used with READFILE or WRITEFILE needs a data type that suits the data: STRING for a line of text, INTEGER for a whole number. A wrong type causes errors.
Practice
DECLARE LineOfText : STRING
Testing
Gaps
DECLARE LineOfText :
Check Answer
Spots
3READFILE "total.txt", Total
4Total ← Total + "10"

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

The 'Armadillo' badge — Shield Bearer, 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 declaration suits a variable that will hold a line of text read from a file?
A. DECLARE LineOfText : INTEGER
B. DECLARE LineOfText : STRING ✓
A line of text is made of characters, so the variable needs the STRING data type.
2. A file stores the whole number 42. Which declaration suits the variable used with READFILE?
A. DECLARE Score : INTEGER ✓
B. DECLARE Score : BOOLEAN
42 is a whole number, so INTEGER is the suitable data type. BOOLEAN only holds TRUE or FALSE.
3. Which statement about the variable used with READFILE is correct?
A. Its data type should suit the data in the file ✓
B. Its data type does not matter, any type works
The data type must suit the data being read, otherwise errors can occur.
4. A student wants to write the line "Hello class" to a file. Which variable type should they declare?
A. STRING ✓
B. INTEGER
"Hello class" is text, so STRING is needed. An INTEGER can only store whole numbers.
5. What is the problem with this code? DECLARE Age : STRING then WRITEFILE "Ages.txt", Age, when Age should be a whole number.
A. WRITEFILE can never be used with a variable
B. The type should be INTEGER to suit a whole number ✓
WRITEFILE can use variables. The error is the mismatched type: a whole number should be stored in an INTEGER variable.

Match the Pairs

DECLARE LineOfText : STRING → Creates a variable that can hold a line of text read from a file or written to a file
DECLARE Score : INTEGER → Creates a variable that can hold a whole number stored in a file
Choose the type when you declare the variable: a line of text needs a STRING variable before it is used with READFILE → DECLARE Name : STRING READFILE "names.txt", Name
A whole number written to a file should be held in an INTEGER variable → DECLARE Total : INTEGER WRITEFILE "total.txt", Total
DECLARE Age : STRING READFILE "ages.txt", Age Total ← Age + 1 → Age holds a whole number but is declared as STRING, so using it in arithmetic causes an error. It should be declared as INTEGER.
DECLARE Line : INTEGER READFILE "poem.txt", Line → Line is meant to hold a line of text but is declared as INTEGER, so the text cannot be stored. It should be declared as STRING.

Key Term

DECLARE LineOfText : STRING
The variable used with READFILE or WRITEFILE needs a data type that suits the data: STRING for a line of text, INTEGER for a whole number. A wrong type causes errors.

Fill in the Blanks

1. DECLARE LineOfText : STRING READFILE "story.txt", LineOfText
Hint: Type the data type that suits a whole line of text read from the file.
2. DECLARE Score : INTEGER READFILE "scores.txt", Score
Hint: Type the data type that suits a stored whole number.
3. DECLARE Mark : INTEGER READFILE "marks.txt", Mark
Hint: Type the name of the declared variable that will hold the data read from the file.
4. DECLARE Total : INTEGER Total ← 250 WRITEFILE "total.txt", Total
Hint: Type the data type that matches the whole number being written to the file.
5. DECLARE Line : STRING OPENFILE "poem.txt" FOR READ READFILE "poem.txt", Line
Hint: Type the command that reads one item from the open file into the variable.

Spot the Error

Question 1
A DECLARE Total : INTEGER
B OPENFILE "total.txt" FOR READ
C READFILE "total.txt", Total
D Total ← Total + "10"
E CLOSEFILE "total.txt"
Line D — Total is an INTEGER read from the file, so you can only add a whole number to it. "10" is a string. It should be Total ← Total + 10.
Question 2
A DECLARE Score : INTEGER
B OPENFILE "score.txt" FOR WRITE
C Score ← "eighty-five"
D WRITEFILE "score.txt", Score
E CLOSEFILE "score.txt"
Line C — Score is declared as INTEGER, so it must hold a whole number. A string cannot be stored in it. It should be Score ← 85.
Question 3
A DECLARE Name : STRING
B DECLARE Age : STRING
C OPENFILE "pupil.txt" FOR READ
D READFILE "pupil.txt", Name
E READFILE "pupil.txt", Age
F OUTPUT Age + 1
Line B — Age holds a whole number and is used in the sum Age + 1, so it must be declared as INTEGER, not STRING. It should be DECLARE Age : INTEGER.
Question 4
A DECLARE Total : INTEGER
B DECLARE Sentence : TEXT
C OPENFILE "story.txt" FOR READ
D READFILE "story.txt", Sentence
E Total ← LENGTH(Sentence)
F OUTPUT Total
Line B — TEXT is not a data type in 0478 pseudocode. A line of text read from a file needs the STRING type. It should be DECLARE Sentence : STRING.
Question 5
A DECLARE Message : STRING
B OPENFILE "msg.txt" FOR WRITE
C Message ← 100
D WRITEFILE "msg.txt", Message
E CLOSEFILE "msg.txt"
Line C — Message is a STRING variable for a line of text, so it cannot be given a whole number. It should be Message ← "Hello".

Student with a class code? Join your class →