← IGCSE Computer Science Paper 2

File modes: READ and WRITE

States what a program will do with an open file: READ gets data out of it, WRITE puts data into it. A file is open in only one mode at a time, chosen by what the program needs.

Understanding File modes: READ and WRITE in IGCSE Computer Science Paper 2

What it does: Getting data out of a file needs READ mode, so FOR READ is written after the file name. In 0478 pseudocode a file is opened in one mode only. To change what you do with it, close it and open it again in the other mode.

Common mistakes: The file was opened FOR READ, so the program can only read from it. It must use READFILE "names.txt", Name to get data out. WRITEFILE needs the file to be open FOR WRITE. The program wants to put Score into the file with WRITEFILE, so the file must be opened FOR WRITE. FOR READ only lets the program get data out of the file.

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 must load saved high scores from a file. Which mode should it use?
READWRITE
Matches
OPENFILE "data.txt" FOR READ WRITEFILE "data.txt", Score
↓
The file is open in READ mode, but the program tries to write data to it. A file opened FOR READ can only be read.
Applying
Meanings
Term
file mode
Meaning
States what a program will do with an open file: READ gets data out of it, WRITE puts data into it. A file is open in only one mode at a time, chosen by what the program needs.
Practice
OPENFILE "Scores.txt" FOR WRITE
Testing
Gaps
OPENFILE "marks.txt" FOR
Check Answer
Spots
2OPENFILE "names.txt" FOR READ
3WRITEFILE "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. A program must load saved high scores from a file. Which mode should it use?
A. READ ✓
B. WRITE
READ gets data out of a file, so use it when the program needs to load data that is already saved.
2. A program must save a player's name into a file. Which mode should it use?
A. READ
B. WRITE ✓
WRITE puts data into a file, so use it when the program needs to save data.
3. Which line opens a file so data can be put into it?
A. OPENFILE "Scores.txt" FOR WRITE ✓
B. OPENFILE "Scores.txt" FOR READ
FOR WRITE means data will be written to the file. FOR READ would only let the program get data out.
4. Which statement about file modes is true?
A. A file mode states what the file is called
B. A file mode states what the program will do with the open file ✓
The mode (READ or WRITE) describes the action the program will take. The file name is the file identifier, a separate idea.
5. Which statement about modes is true?
A. A file is open in only one mode at a time ✓
B. A file can be open in READ and WRITE mode at the same time
In 0478 pseudocode a file is opened in one mode only. To change what you do with it, close it and open it again in the other mode.

Match the Pairs

OPENFILE "scores.txt" FOR READ → Opens the file in READ mode so the program can get data out of it
OPENFILE "scores.txt" FOR WRITE → Opens the file in WRITE mode so the program can put data into it
The program needs to get saved data out of a file, so it chooses READ mode → OPENFILE "names.txt" FOR READ
The program needs to save a new high score into a file, so it chooses WRITE mode → OPENFILE "best.txt" FOR WRITE
OPENFILE "data.txt" FOR READ WRITEFILE "data.txt", Score → The file is open in READ mode, but the program tries to write data to it. A file opened FOR READ can only be read.
OPENFILE "data.txt" FOR WRITE READFILE "data.txt", Score → The file is open in WRITE mode, but the program tries to read data from it. A file opened FOR WRITE can only be written to.

Key Term

file mode
States what a program will do with an open file: READ gets data out of it, WRITE puts data into it. A file is open in only one mode at a time, chosen by what the program needs.

Fill in the Blanks

1. OPENFILE "marks.txt" FOR WRITE WRITEFILE "marks.txt", Score CLOSEFILE "marks.txt"
Hint: Type the file mode used when the program puts data into a file.
2. OPENFILE "names.txt" FOR READ READFILE "names.txt", Name CLOSEFILE "names.txt"
Hint: Type the file mode used when the program gets data out of a file.
3. OPENFILE "log.txt" FOR READ READFILE "log.txt", Entry CLOSEFILE "log.txt"
Hint: Type the keyword that comes before the file mode in an open statement.
4. OPENFILE "data.txt" FOR READ READFILE "data.txt", Item CLOSEFILE "data.txt"
Hint: Type the command that opens a file and states its mode.
5. // Load the saved score OPENFILE "best.txt" FOR READ READFILE "best.txt", HighScore
Hint: Choose the mode that matches the comment: the program needs to get data out of the file.

Spot the Error

Question 1
A DECLARE Name : STRING
B OPENFILE "names.txt" FOR READ
C WRITEFILE "names.txt", Name
D CLOSEFILE "names.txt"
Line C — The file was opened FOR READ, so the program can only read from it. It must use READFILE "names.txt", Name to get data out. WRITEFILE needs the file to be open FOR WRITE.
Question 2
A DECLARE Score : INTEGER
B OPENFILE "data.txt" FOR READ
C WRITEFILE "data.txt", Score
D CLOSEFILE "data.txt"
Line B — The program wants to put Score into the file with WRITEFILE, so the file must be opened FOR WRITE. FOR READ only lets the program get data out of the file.
Question 3
A OPENFILE "pets.txt" FOR WRITE
B WRITEFILE "pets.txt", "Cat"
C READFILE "pets.txt", Pet
D CLOSEFILE "pets.txt"
Line C — The file is open in WRITE mode, so the program cannot read from it. Line 3 should be WRITEFILE "pets.txt", "Dog" to put the next item into the file.
Question 4
A OPENFILE "a.txt" FOR READ
B READFILE "a.txt", Line
C OPENFILE "a.txt" FOR WRITE
D OPENFILE "a.txt" FOR WRITE
E WRITEFILE "a.txt", Line
Line C — A file is open in only one mode at a time. The READ file must be closed with CLOSEFILE "a.txt" before it is opened again FOR WRITE on line 4.
Question 5
A DECLARE Total : INTEGER
B OPENFILE "total.txt" READ
C READFILE "total.txt", Total
D OUTPUT Total
Line B — The file mode must be introduced with the keyword FOR. The correct line is OPENFILE "total.txt" FOR READ.

Student with a class code? Join your class →