← IGCSE Computer Science Paper 2

WRITE mode creates a new file and replaces old data

Opens a file for writing. A new file is created if none exists. If it does exist, all its old data is erased and replaced, so check before overwriting data you need.

Understanding WRITE mode creates a new file and replaces old data in IGCSE Computer Science Paper 2

What it does: WRITE mode creates the file when it is not already there, so the program can start saving data straight away. Opening Results.txt FOR WRITE would erase last week's results. A new file name leaves the old file untouched. Opens a file for writing. A new file is created if none exists. If it does exist, all its old data is erased and replaced, so check before overwriting data you need.

Common mistakes: The file name is a string, so it must be in quotes: WRITEFILE "Data.txt", "Liam". Without quotes it looks like a variable, not the file that was opened for WRITE. READ mode only reads an existing file. It does not create a new file or replace old data. To save the score the file must be opened 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
A program runs OPENFILE "Scores.txt" FOR WRITE and no file called Scores.txt exists yet. What happens?
A new, empty file called Scores.txt is createdThe program stops with an error because the file is missing
Matches
// Scores.txt holds 50 saved scores OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", NewScore
↓
Opening Scores.txt for WRITE erases the 50 saved scores, leaving only NewScore. The old data should have been read or copied first.
Applying
Meanings
Term
OPENFILE "Scores.txt" FOR WRITE
Meaning
Opens a file for writing. A new file is created if none exists. If it does exist, all its old data is erased and replaced, so check before overwriting data you need.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
OPENFILE "Scores.txt" FOR
Check Answer
Spots
2WRITEFILE "Data.txt", "Ava"
3WRITEFILE Data.txt, "Liam"

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

The 'Flamingo' badge — Balanced Coder, 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. A program runs OPENFILE "Scores.txt" FOR WRITE and no file called Scores.txt exists yet. What happens?
A. The program stops with an error because the file is missing
B. A new, empty file called Scores.txt is created ✓
WRITE mode creates a new file if none exists with that name. The file starts empty and is ready for data.
2. Scores.txt already holds 50 lines of data. A program runs OPENFILE "Scores.txt" FOR WRITE. What happens to the old data?
A. It is kept and new data is added after it
B. It is erased and replaced ✓
Opening an existing file for WRITE wipes its old contents. New data replaces the old data and is not added to it.
3. Which statement about FOR WRITE is correct?
A. Opening an existing file FOR WRITE deletes what was in it ✓
B. Opening an existing file FOR WRITE keeps its data safe until CLOSEFILE
The old data is lost as soon as the file is opened for WRITE, before any WRITEFILE statement runs.
4. A program runs OPENFILE "Names.txt" FOR WRITE twice, writing different data each time. What does Names.txt contain at the end?
A. Only the data from the second run ✓
B. The data from both runs together
Opening the same file for WRITE again wipes it. Only the most recent data remains.
5. Which approach protects data you still need?
A. Check whether the file already exists, or use a different file name, before opening it FOR WRITE ✓
B. Open the file FOR WRITE first and hope the old data is still there
Because WRITE replaces old data, you should check before overwriting a file you need. A different file name keeps the old file safe.

Match the Pairs

OPENFILE "Scores.txt" FOR WRITE → Creates Scores.txt if it does not exist; if it does exist, all the old data in it is erased
OPENFILE "Log.txt" FOR WRITE (run a second time) → Wipes everything saved in Log.txt by the first run, so only new data will be there
If no file called Names.txt exists, opening it for WRITE makes a new empty file with that name → OPENFILE "Names.txt" FOR WRITE
To keep the old data in Scores.txt, read it first (READ mode) before opening the file for WRITE → OPENFILE "Scores.txt" FOR READ
// Scores.txt holds 50 saved scores OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", NewScore → Opening Scores.txt for WRITE erases the 50 saved scores, leaving only NewScore. The old data should have been read or copied first.
// Day 1 data is saved in Diary.txt OPENFILE "Diary.txt" FOR WRITE WRITEFILE "Diary.txt", Day2Text → The programmer wrongly thinks Day2Text will be added after the Day 1 data. Opening for WRITE deletes the Day 1 data first.

Key Term

OPENFILE "Scores.txt" FOR WRITE
Opens a file for writing. A new file is created if none exists. If it does exist, all its old data is erased and replaced, so check before overwriting data you need.

Fill in the Blanks

1. OPENFILE "Scores.txt" FOR WRITE
Hint: Type the file mode keyword that creates a new file, or replaces the old data if the file already exists.
2. OPENFILE "Scores.txt" FOR WRITE
Hint: Type the command that opens a file before it can be written to.
3. OPENFILE "Log.txt" FOR WRITE WRITEFILE "Log.txt", "Day 1" CLOSEFILE "Log.txt"
Hint: Type the command that closes the file once the new data has been written.
4. OPENFILE "New.txt" FOR WRITE
Hint: Type the keyword that links the file name to the mode it is opened in.
5. Score ← 50 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Hint: Type the variable name that holds the value being written, replacing any old data in the file.

Spot the Error

Question 1
A OPENFILE "Data.txt" FOR WRITE
B WRITEFILE "Data.txt", "Ava"
C WRITEFILE Data.txt, "Liam"
D CLOSEFILE "Data.txt"
Line C — The file name is a string, so it must be in quotes: WRITEFILE "Data.txt", "Liam". Without quotes it looks like a variable, not the file that was opened for WRITE.
Question 2
A OUTPUT "Saving best score"
B OPENFILE "Best.txt" FOR READ
C WRITEFILE "Best.txt", Score
D CLOSEFILE "Best.txt"
Line B — READ mode only reads an existing file. It does not create a new file or replace old data. To save the score the file must be opened FOR WRITE.
Question 3
A DECLARE Name : STRING
B OPENFILE "Names.txt" FOR WRITE
C WRITEFILE "Names.txt", Name
D CLOSE "Names.txt"
Line D — The pseudocode keyword is CLOSEFILE, not CLOSE. It should be CLOSEFILE "Names.txt" so the new file is saved properly.
Question 4
A INPUT Name
B OPENFILE "Log.txt" TO WRITE
C WRITEFILE "Log.txt", Name
D CLOSEFILE "Log.txt"
Line B — The file mode is introduced with the keyword FOR, not TO. It should be OPENFILE "Log.txt" FOR WRITE. If Log.txt is missing it is created, otherwise its old data is replaced.
Question 5
A OPENFILE "Log.txt" FOR WRITE
B WRITEFILE "Log.txt" "Day 1"
C CLOSEFILE "Log.txt"
D OPENFILE "Log.txt" FOR WRITE
E WRITEFILE "Log.txt", "Day 2"
Line B — A comma is needed between the file name and the data: WRITEFILE "Log.txt", "Day 1". Note that opening Log.txt FOR WRITE again on line 4 wipes Day 1, so only Day 2 remains.

Student with a class code? Join your class →