← IGCSE Computer Science Paper 2

WRITEFILE: writing a single item of data

Writes the value stored in a variable into an open file. Give the file name, a comma, then the variable. The file must already be open in WRITE mode.

Understanding WRITEFILE: writing a single item of data in IGCSE Computer Science Paper 2

What it does: WRITEFILE only works on a file that is already open in WRITE mode, so OPENFILE ... FOR WRITE comes first. The order is file name string, comma, variable. Here "Marks.txt" is the file identifier and Mark is the variable. WRITEFILE writes the value stored in the variable, not the variable's name. The two items must be separated by a comma: WRITEFILE "Scores.txt", Score.

Common mistakes: WRITEFILE only works on a file opened in WRITE mode. Line 2 opens the file FOR READ, so it should be OPENFILE "Scores.txt" FOR WRITE. The file identifier is a string, so the file name must be in quotes. It should be OPENFILE "Names.txt" FOR WRITE.

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 correctly writes the value of the variable Score into the file Scores.txt?
WRITEFILE "Scores.txt", ScoreWRITEFILE Score, "Scores.txt"
Matches
WRITEFILE Score, "Scores.txt"
↓
The arguments are the wrong way round. The file identifier must come first, then the comma, then the variable.
Applying
Meanings
Term
WRITEFILE "Scores.txt", Score
Meaning
Writes the value stored in a variable into an open file. Give the file name, a comma, then the variable. The file must already be open in WRITE mode.
Practice
DECLARE Score : INTEGER
Testing
Gaps
"Scores.txt", Score
Check Answer
Spots
1Score ← 85
2OPENFILE "Scores.txt" FOR READ

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

The 'Moray Eel' badge — Lurking 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 line correctly writes the value of the variable Score into the file Scores.txt?
A. WRITEFILE "Scores.txt", Score ✓
B. WRITEFILE Score, "Scores.txt"
The file identifier comes first, then a comma, then the variable whose value is written.
2. True or false: WRITEFILE can write to a file that has been opened only in READ mode.
A. True
B. False ✓
The file must already be open in WRITE mode before WRITEFILE can be used.
3. Score is an INTEGER variable holding 42. What does WRITEFILE "Scores.txt", Score put in the file?
A. The value 42 ✓
B. The word Score
WRITEFILE writes the value stored in the variable, not the variable's name.
4. Which order is needed for the two items after WRITEFILE?
A. File identifier, then a comma, then the variable ✓
B. Variable, then a comma, then the file identifier
The syntax is WRITEFILE <File identifier>, <Variable>.
5. Which program order is correct?
A. WRITEFILE "Scores.txt", Score, then OPENFILE "Scores.txt" FOR WRITE
B. OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE "Scores.txt", Score ✓
The file must be opened in WRITE mode before any data can be written to it.

Match the Pairs

WRITEFILE "Scores.txt", Score → Writes the value stored in the variable Score into the open file Scores.txt
WRITEFILE "Names.txt", PlayerName → Writes the STRING held in PlayerName into the open file Names.txt
Open the file in WRITE mode first, then use WRITEFILE to store the value of Score → OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
The file identifier comes first, then a comma, then the variable to write → WRITEFILE "Marks.txt", Mark
WRITEFILE Score, "Scores.txt" → The arguments are the wrong way round. The file identifier must come first, then the comma, then the variable.
WRITEFILE "Scores.txt" Score → The comma between the file identifier and the variable is missing.

Key Term

WRITEFILE "Scores.txt", Score
Writes the value stored in a variable into an open file. Give the file name, a comma, then the variable. The file must already be open in WRITE mode.

Fill in the Blanks

1. WRITEFILE "Scores.txt", Score
Hint: Type the keyword that writes a variable's value into an open file.
2. Score ← 85 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the name of the INTEGER variable whose value is written to the file.
3. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the file mode the file must be opened in before you can write to it.
4. Name ← "Ava" OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name
Hint: Type the name of the STRING variable that holds the item to write.
5. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the file name (the quotes are already there) that matches the file opened on the line above.

Spot the Error

Question 1
A Score ← 85
B OPENFILE "Scores.txt" FOR READ
C WRITEFILE "Scores.txt", Score
D CLOSEFILE "Scores.txt"
Line B — WRITEFILE only works on a file opened in WRITE mode. Line 2 opens the file FOR READ, so it should be OPENFILE "Scores.txt" FOR WRITE.
Question 2
A Name ← "Ava"
B OPENFILE Names.txt FOR WRITE
C WRITEFILE "Names.txt", Name
D CLOSEFILE "Names.txt"
Line B — The file identifier is a string, so the file name must be in quotes. It should be OPENFILE "Names.txt" FOR WRITE.
Question 3
A Score ← 72
B OPENFILE "Scores.txt" FOR WRITE
C Score ← Score + 1
D WRITEFILE "Scores.txt" Score
E CLOSEFILE "Scores.txt"
Line D — A comma is needed between the file identifier and the variable. It should be WRITEFILE "Scores.txt", Score.
Question 4
A Age ← 16
B OPENFILE "Ages.txt" FOR WRITE
C WRITEFILE Age, "Ages.txt"
D CLOSEFILE "Ages.txt"
Line C — The order is wrong. The file identifier comes first, then a comma, then the variable. It should be WRITEFILE "Ages.txt", Age.
Question 5
A Name ← "Zoe"
B OPENFILE "Players.txt" FOR WRITE
C WRITEFILE "Player.txt", Name
D CLOSEFILE "Players.txt"
Line C — The file identifier must match the name used in OPENFILE exactly. "Player.txt" is not the open file "Players.txt", so line 3 should use "Players.txt".

Student with a class code? Join your class →