← IGCSE Computer Science Paper 2

OPENFILE ... FOR WRITE

Opens the file Scores.txt in write mode so data can be written into it with WRITEFILE. The file must be opened before writing. Keywords are in capitals.

Understanding OPENFILE ... FOR WRITE in IGCSE Computer Science Paper 2

What it does: The file identifier "Results.txt" is a string, and FOR WRITE states the mode explicitly, which is good practice. 0478 pseudocode writes keywords in capitals, so OPENFILE, FOR and WRITE are all uppercase. A file must be opened before you write to it. Opening comes first, then WRITEFILE. FOR WRITE opens a file for writing. FOR READ opens a file for reading. The keyword is OPENFILE, not OPEN.

Common mistakes: The file was opened FOR WRITE, so data must be put into it with WRITEFILE. READFILE reads data out of a file. The line should be WRITEFILE "Names.txt", Name. The program wants to write Score into the file, so the file must be opened in write mode. FOR READ only allows reading. It should be OPENFILE "Data.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 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 opens the file Scores.txt so data can be written to it?
OPENFILE Scores.txt FOR WRITEOPENFILE "Scores.txt" FOR WRITE
Matches
WRITEFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR WRITE
↓
WRITEFILE is used before the file is opened. OPENFILE ... FOR WRITE must come first.
Applying
Meanings
Term
OPENFILE "Scores.txt" FOR WRITE
Meaning
Opens the file Scores.txt in write mode so data can be written into it with WRITEFILE. The file must be opened before writing. Keywords are in capitals.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
"Scores.txt" FOR WRITE
Check Answer
Spots
3OPENFILE "Names.txt" FOR WRITE
4READFILE "Names.txt", Name

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

The 'Desert Jerboa' badge — High Jumper, 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 opens the file Scores.txt so data can be written to it?
A. OPENFILE Scores.txt FOR WRITE
B. OPENFILE "Scores.txt" FOR WRITE ✓
The file identifier is a string, so it goes in double quotes: "Scores.txt".
2. What does FOR WRITE tell the program in OPENFILE "Scores.txt" FOR WRITE?
A. The file is being opened so data can be written into it ✓
B. The file is being opened so data can be read from it
FOR WRITE is the file mode. It means the program will put data into the file. FOR READ is used to read data.
3. Which order of statements is correct?
A. WRITEFILE, then OPENFILE "Scores.txt" FOR WRITE
B. OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE ✓
A file must be opened before you write to it. Opening comes first, then WRITEFILE.
4. Which is the correct way to write the keywords in 0478 pseudocode?
A. OPENFILE "Scores.txt" FOR WRITE ✓
B. openfile "Scores.txt" for write
Keywords in 0478 pseudocode are written in capital letters: OPENFILE, FOR and WRITE.
5. True or false: it is good practice to state the mode (FOR WRITE) every time you open a file.
A. True: the mode makes clear what the program will do with the file ✓
B. False: the mode can be left out when opening a file
Stating the mode explicitly makes the code clear and shows exactly what the file is being used for.

Match the Pairs

OPENFILE "Scores.txt" FOR WRITE → Opens the file Scores.txt in write mode so data can be written into it
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name → The file is opened first, then data is written into it
The file name is a string in quotes, then the mode FOR WRITE states that you will write to the file → OPENFILE "Results.txt" FOR WRITE
Keywords such as OPENFILE and FOR WRITE are written in capital letters in 0478 pseudocode → OPENFILE "Marks.txt" FOR WRITE
WRITEFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR WRITE → WRITEFILE is used before the file is opened. OPENFILE ... FOR WRITE must come first.
OPENFILE Scores.txt FOR WRITE → The file name is missing its quotation marks. The file identifier must be a string such as "Scores.txt".

Key Term

OPENFILE "Scores.txt" FOR WRITE
Opens the file Scores.txt in write mode so data can be written into it with WRITEFILE. The file must be opened before writing. Keywords are in capitals.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR WRITE
Hint: Type the keyword that opens a file before you use it.
2. OPENFILE "Scores.txt" FOR WRITE
Hint: Type the keyword that sits between the file name and the mode.
3. OPENFILE "Scores.txt" FOR WRITE
Hint: Type the file mode, in capitals, that lets you put data into the file.
4. OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the command that puts data into the file once it has been opened.
5. OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name
Hint: The next line writes data, so type the mode the file must be opened in.

Spot the Error

Question 1
A DECLARE Name : STRING
B Name ← "Ava"
C OPENFILE "Names.txt" FOR WRITE
D READFILE "Names.txt", Name
E CLOSEFILE "Names.txt"
Line D — The file was opened FOR WRITE, so data must be put into it with WRITEFILE. READFILE reads data out of a file. The line should be WRITEFILE "Names.txt", Name.
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Data.txt" FOR READ
C Score ← 85
D WRITEFILE "Data.txt", Score
E CLOSEFILE "Data.txt"
Line B — The program wants to write Score into the file, so the file must be opened in write mode. FOR READ only allows reading. It should be OPENFILE "Data.txt" FOR WRITE.
Question 3
A DECLARE Mark : INTEGER
B Mark ← 72
C OPENFILE "Marks.txt" FOR WRITE
D WRITEFILE Mark, "Marks.txt"
E CLOSEFILE "Marks.txt"
Line D — WRITEFILE needs the file identifier first, then a comma, then the data to write. The arguments are the wrong way round. It should be WRITEFILE "Marks.txt", Mark.
Question 4
A DECLARE Item : STRING
B Item ← "Pen"
C OPEN "Shop.txt" FOR WRITE
D WRITEFILE "Shop.txt", Item
E CLOSEFILE "Shop.txt"
Line C — The 0478 pseudocode keyword is OPENFILE, not OPEN. The line should be OPENFILE "Shop.txt" FOR WRITE.
Question 5
A DECLARE Age : INTEGER
B OPENFILE "Ages.txt" WRITE
C Age ← 16
D WRITEFILE "Ages.txt", Age
E CLOSEFILE "Ages.txt"
Line B — The keyword FOR is missing before the mode. The correct form is OPENFILE <File identifier> FOR WRITE, so the line should be OPENFILE "Ages.txt" FOR WRITE.

Student with a class code? Join your class →