← IGCSE Computer Science Paper 2

WRITEFILE: writing a line of text

Writes the text held in the STRING variable LineOfText into the file Names.txt as one new line. Each WRITEFILE adds one more line to the file.

Understanding WRITEFILE: writing a line of text in IGCSE Computer Science Paper 2

What it does: Every WRITEFILE statement writes one line, so two statements give two lines in the file. In 0478 pseudocode the file identifier comes first, then a comma, then the variable holding the data to write. A line of text is a STRING. An INTEGER variable can only hold whole numbers. LineOfText is a variable name, so WRITEFILE writes the text stored inside it. It does not write the name itself. The assignment arrow points left (←), towards the variable receiving the value. The file identifier is a string, so it needs quote marks around it: "Names.txt".

Common mistakes: Line holds a line of text, so it must be declared as STRING, not INTEGER. It should read DECLARE Line : STRING. The program writes a line to the file, so the file must be opened FOR WRITE, not FOR READ. READ mode cannot be used with WRITEFILE.

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 writes the text held in the STRING variable LineOfText to the file Names.txt?
WRITEFILE LineOfText, "Names.txt"WRITEFILE "Names.txt", LineOfText
Matches
WRITEFILE LineOfText, "Names.txt"
↓
The arguments are in the wrong order. The file name must come first, then the variable holding the line of text.
Applying
Meanings
Term
WRITEFILE "Names.txt", LineOfText
Meaning
Writes the text held in the STRING variable LineOfText into the file Names.txt as one new line. Each WRITEFILE adds one more line to the file.
Practice
DECLARE LineOfText : STRING
Testing
Gaps
"Names.txt", LineOfText
Check Answer
Spots
1DECLARE Line : INTEGER
2OPENFILE "Names.txt" FOR WRITE

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

The 'Kangaroo Rat' badge — Speed Typer, 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 writes the text held in the STRING variable LineOfText to the file Names.txt?
A. WRITEFILE "Names.txt", LineOfText ✓
B. WRITEFILE LineOfText, "Names.txt"
In 0478 pseudocode the file identifier comes first, then a comma, then the variable holding the data to write.
2. A program runs WRITEFILE "Names.txt", LineOfText three times in a row. How many lines does it add to the file?
A. 1 line, because the file name is the same
B. 3 lines, one for each WRITEFILE ✓
Each WRITEFILE adds one more line to the file, even when the file name and variable are the same.
3. Which variable declaration suits a variable that will hold a line of text to write to a file?
A. DECLARE LineOfText : INTEGER
B. DECLARE LineOfText : STRING ✓
A line of text is a STRING. An INTEGER variable can only hold whole numbers.
4. Which order is correct for writing a name typed in by the user?
A. INPUT LineOfText, then WRITEFILE "Names.txt", LineOfText ✓
B. WRITEFILE "Names.txt", LineOfText, then INPUT LineOfText
The variable must be given its text first. If WRITEFILE comes before INPUT, the variable has no value yet.
5. Which statement is true about WRITEFILE "Names.txt", LineOfText?
A. It writes the value stored in LineOfText into the file ✓
B. It writes the word LineOfText into the file
LineOfText is a variable name, so WRITEFILE writes the text stored inside it. It does not write the name itself.

Match the Pairs

WRITEFILE "Names.txt", LineOfText → Writes the text held in the STRING variable LineOfText into Names.txt as one new line
LineOfText ← "Amira Khan" → Stores a line of text in a STRING variable so it is ready to be written to the file
Each WRITEFILE adds one more line to the file, so two WRITEFILE statements put two lines in it → WRITEFILE "Names.txt", LineOfText WRITEFILE "Names.txt", LineOfText
The line of text can come from the user: read it with INPUT into a STRING variable, then write it → INPUT LineOfText WRITEFILE "Names.txt", LineOfText
WRITEFILE LineOfText, "Names.txt" → The arguments are in the wrong order. The file name must come first, then the variable holding the line of text.
WRITEFILE "Names.txt" LineOfText → The comma between the file name and the variable is missing.

Key Term

WRITEFILE "Names.txt", LineOfText
Writes the text held in the STRING variable LineOfText into the file Names.txt as one new line. Each WRITEFILE adds one more line to the file.

Fill in the Blanks

1. OPENFILE "Names.txt" FOR WRITE LineOfText ← "Ava Nguyen" WRITEFILE "Names.txt", LineOfText
Hint: Type the command that writes the text in the variable into the open file as a new line.
2. LineOfText ← "Mia Smith" WRITEFILE "Names.txt", LineOfText
Hint: Type the name of the variable that holds the line of text to be written.
3. OPENFILE "Names.txt" FOR WRITE LineOfText ← "Leo Jones" WRITEFILE "Names.txt", LineOfText
Hint: Type the file name, without the quote marks, of the file that was opened for writing.
4. OPENFILE "Names.txt" FOR WRITE INPUT LineOfText WRITEFILE "Names.txt", LineOfText
Hint: Type the keyword that takes the line of text from the user and stores it in the variable.
5. DECLARE LineOfText : STRING LineOfText ← "Zoe Brown" WRITEFILE "Names.txt", LineOfText
Hint: Type the data type that a variable holding a line of text must have.

Spot the Error

Question 1
A DECLARE Line : INTEGER
B OPENFILE "Names.txt" FOR WRITE
C Line ← "Ava Lee"
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"
Line A — Line holds a line of text, so it must be declared as STRING, not INTEGER. It should read DECLARE Line : STRING.
Question 2
A OPENFILE "Names.txt" FOR READ
B Line ← "Mia Chen"
C WRITEFILE "Names.txt", Line
D CLOSEFILE "Names.txt"
Line A — The program writes a line to the file, so the file must be opened FOR WRITE, not FOR READ. READ mode cannot be used with WRITEFILE.
Question 3
A DECLARE Line : STRING
B OPENFILE "Names.txt" FOR WRITE
C Line ← "Noah Khan"
D WRITEFILE "Names.txt", "Line"
E CLOSEFILE "Names.txt"
Line D — Putting Line in quotes would write the word Line into the file. The variable name must not be in quotes so that its text (Noah Khan) is written: WRITEFILE "Names.txt", Line.
Question 4
A DECLARE Line : STRING
B OUTPUT Line
C OPENFILE "Names.txt" FOR WRITE
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"
Line B — OUTPUT only displays a value. To take the line of text from the user and store it in Line, you must use INPUT Line.
Question 5
A DECLARE Line : STRING
B OPENFILE "Names.txt" FOR WRITE
C Line ← Zoe Park
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"
Line C — A text value assigned to a STRING variable must be inside double quotes. It should be Line ← "Zoe Park".

Student with a class code? Join your class →