← IGCSE Computer Science Paper 2

Writing a complete file handling program in 0478 pseudocode

The order of a complete file program: declare variables, open the file in the right mode, read or write the data, then close the file. Keywords are in capitals and statements are indented.

Understanding Writing a complete file handling program in 0478 pseudocode in IGCSE Computer Science Paper 2

What it does: OPENFILE comes after DECLARE and before WRITEFILE. The mode FOR WRITE matches the job of saving data into the file. Comments starting with // and meaningful identifiers make a program easy to understand. In an exam answer, they show what the file is used for. The file identifier is a string, so it goes in double quotes. The Cambridge pseudocode layout uses capitals for keywords. A good comment states the purpose of the code. Vague comments do not help the reader. Describe the purpose of the file: data kept in a file is not lost when the program stops, unlike data held in variables.

Common mistakes: The program writes data with WRITEFILE, so the file must be opened in WRITE mode. Line 2 should be OPENFILE "names.txt" FOR WRITE. OPENFILE needs the file identifier (the file name as a string) before the mode. It should be OPENFILE "diary.txt" FOR READ.

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 500–522) and word search (Modules 500–538).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which shows the correct order of a complete file-writing program?
DECLARE, OPENFILE, WRITEFILE, CLOSEFILEOPENFILE, WRITEFILE, DECLARE, CLOSEFILE
Matches
OPENFILE "Names.txt" FOR WRITE DECLARE Name : STRING WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
↓
The statements are in the wrong order. OPENFILE comes before DECLARE, but the variable should be declared first.
Applying
Meanings
Term
DECLARE, OPENFILE, WRITEFILE, CLOSEFILE
Meaning
The order of a complete file program: declare variables, open the file in the right mode, read or write the data, then close the file. Keywords are in capitals and statements are indented.
Practice
// Save three scores to a file
Testing
Gaps
PlayerName : STRING
Check Answer
Spots
1DECLARE Name : STRING
2OPENFILE "names.txt" FOR READ

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

The 'Sea Dragon' badge — OOP Pioneer, 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 shows the correct order of a complete file-writing program?
A. OPENFILE, WRITEFILE, DECLARE, CLOSEFILE
B. DECLARE, OPENFILE, WRITEFILE, CLOSEFILE ✓
Declare variables first, then open the file, write the data, and close the file last.
2. Which line opens a file so a program can save data 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.
3. Which is the better identifier for a variable holding a player's name that will be saved to a file?
A. x
B. PlayerName ✓
Meaningful identifiers make the program easier to read and are expected in exam answers.
4. In 0478 pseudocode, how should keywords such as DECLARE and OPENFILE be written?
A. In capital letters ✓
B. In lower case
The Cambridge pseudocode layout uses capitals for keywords.
5. Where should the CLOSEFILE statement go in a complete file program?
A. After the last read or write ✓
B. Straight after DECLARE
The file must stay open while data is being read or written, and be closed once the program has finished with it.

Match the Pairs

DECLARE Name : STRING → Step 1: declare a variable to hold the data that will be written to or read from the file.
CLOSEFILE("Scores.txt") → The final step: close the file once all the reading or writing is finished so the data is safely saved.
After declaring variables, open the file in WRITE mode so data can be stored in it → OPENFILE "Names.txt" FOR WRITE
A comment explains the purpose of the file, and keywords are in capitals → // Store the high scores for the quiz game
OPENFILE "Names.txt" FOR WRITE DECLARE Name : STRING WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt" → The statements are in the wrong order. OPENFILE comes before DECLARE, but the variable should be declared first.
DECLARE Name : STRING OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name → The program never closes the file. CLOSEFILE "Names.txt" is missing from the end.

Key Term

DECLARE, OPENFILE, WRITEFILE, CLOSEFILE
The order of a complete file program: declare variables, open the file in the right mode, read or write the data, then close the file. Keywords are in capitals and statements are indented.

Fill in the Blanks

1. DECLARE PlayerName : STRING OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName
Hint: Type the keyword that creates a variable before the file is opened.
2. DECLARE PlayerName : STRING OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName
Hint: Type the file mode used when the program saves data into the file.
3. OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName CLOSEFILE "Players.txt"
Hint: Type the keyword that must come last to finish with the file.
4. OPENFILE "Players.txt" FOR READ READFILE "Players.txt", PlayerName CLOSEFILE "Players.txt"
Hint: Type the keyword that takes data from the open file into the variable.
5. DECLARE Score : INTEGER OPENFILE "Score.txt" FOR WRITE WRITEFILE "Score.txt", Score
Hint: Type the variable declared on the first line whose value is saved to 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 B — The program writes data with WRITEFILE, so the file must be opened in WRITE mode. Line 2 should be OPENFILE "names.txt" FOR WRITE.
Question 2
A DECLARE Line : STRING
B OPENFILE FOR READ
C READFILE "diary.txt", Line
D OUTPUT Line
E CLOSEFILE "diary.txt"
Line B — OPENFILE needs the file identifier (the file name as a string) before the mode. It should be OPENFILE "diary.txt" FOR READ.
Question 3
A DECLARE Score : INTEGER
B OPENFILE "score.txt" FOR WRITE
C WRITEFILE Score, "score.txt"
D CLOSEFILE "score.txt"
Line C — WRITEFILE takes the file name first, then a comma, then the data. It should be WRITEFILE "score.txt", Score.
Question 4
A DECLARE Word : STRING
B OPENFILE "words.txt" FOR READ
C READFILE "words.txt"
D OUTPUT Word
E CLOSEFILE "words.txt"
Line C — READFILE needs a variable to store the data it reads. It should be READFILE "words.txt", Word.
Question 5
A // Save the pupil's name
B DECLARE Pupil STRING
C OPENFILE "pupil.txt" FOR WRITE
D WRITEFILE "pupil.txt", Pupil
E CLOSEFILE "pupil.txt"
Line B — A DECLARE statement needs a colon between the identifier and the data type. It should be DECLARE Pupil : STRING.

Student with a class code? Join your class →