Revision Notes
Cambridge IGCSE Computer Science (0478): File handling – Revision Notes
Revision notes for Cambridge IGCSE Computer Science 0478 section 8.3: OPENFILE, READFILE, WRITEFILE and CLOSEFILE, file modes and common file patterns.
- Subject
- Computer Science
- Level
- IGCSE
- Topic
- Topic 8: Programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to Cambridge IGCSE Computer Science (0478), 2026-2028. Official specification .
Syllabus page (what it covers and how it is assessed): Cambridge IGCSE Computer Science.
Syllabus points this page covers
0478
- 8.3 File handling
This page covers 1 of the 3 subtopics in topic 8, Programming.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for IGCSE Computer Science (0478).
Condensed notes for syllabus section 8.3, File handling: why programs use files and how to use the four file commands.
Four facts to start with
- A file stores data on secondary storage, so it survives when the program ends.
OPENFILEprepares a file forREADorWRITE.READFILEandWRITEFILEdeal with one data item (or one line of text) at a time.CLOSEFILEfinishes with the file. Opening inWRITEmode erases existing data.
Key terms
| Term | Definition |
|---|---|
| File | Named collection of data stored on secondary storage |
| File identifier | Name of the file, written as a string |
| File mode | Whether the file is open for READ or WRITE |
| Read | Copy a data item from a file into a variable |
| Write | Copy the value of a variable into a file |
| Volatile | Loses its contents when power is off, like RAM |
Why use a file?
- Data is kept after the program ends.
- Large amounts of data can be stored.
- Data can be shared with other programs.
- The data can be changed without changing the program.
The four commands
| Command | Example |
|---|---|
| Open | OPENFILE "Scores.txt" FOR READ |
| Read | READFILE "Scores.txt", Score |
| Write | WRITEFILE "Scores.txt", Score |
| Close | CLOSEFILE "Scores.txt" |
File modes
| Mode | Use | Warning |
|---|---|---|
READ |
Take data from the file | File must exist |
WRITE |
Put data into the file | Existing data is lost |
Only one mode at a time for one file.
Standard patterns
Write a list:
OPENFILE "List.txt" FOR WRITE
FOR Index ← 1 TO 5
WRITEFILE "List.txt", Items[Index]
NEXT Index
CLOSEFILE "List.txt"
Read a list into an array: the same loop with READFILE "List.txt", Items[Index] and mode READ.
Copy a line: open both files, READFILE into a string variable, WRITEFILE that variable to the other file, close both files.
Worked example: total from a file
Marks.txt holds 12, 18, 9 (one per line).
Total ← 0
OPENFILE "Marks.txt" FOR READ
FOR Index ← 1 TO 3
READFILE "Marks.txt", Mark
Total ← Total + Mark
NEXT Index
CLOSEFILE "Marks.txt"
OUTPUT Total
| Index | Mark | Total |
|---|---|---|
| 1 | 12 | 12 |
| 2 | 18 | 30 |
| 3 | 9 | 39 |
The output is 39.
Worked example: what is in the file
OPENFILE "Test.txt" FOR WRITE
WRITEFILE "Test.txt", "Red"
WRITEFILE "Test.txt", "Blue"
CLOSEFILE "Test.txt"
OPENFILE "Test.txt" FOR WRITE
WRITEFILE "Test.txt", "Green"
CLOSEFILE "Test.txt"
After the first block the file holds Red and Blue. The second OPENFILE ... FOR WRITE replaces the contents, so the file finally holds only Green.
Updating a file without losing data
- Open in
READ; read all items into an array; close. - Add the new item to the array.
- Open in
WRITE; write every item from the array; close.
Example: Names.txt holds Ali, Bea, Cai (three names). Read them into Names[1] to Names[3], add the fourth name, then write all four back:
OPENFILE "Names.txt" FOR READ
FOR Index ← 1 TO 3
READFILE "Names.txt", Names[Index]
NEXT Index
CLOSEFILE "Names.txt"
Names[4] ← "Dev"
OPENFILE "Names.txt" FOR WRITE
FOR Index ← 1 TO 4
WRITEFILE "Names.txt", Names[Index]
NEXT Index
CLOSEFILE "Names.txt"
The file now holds Ali, Bea, Cai, Dev. The first loop runs 3 times (one per name already in the file) and the second runs 4 times (every item must be written again because WRITE replaced the old contents).
Variable, array or file?
| Need | Use |
|---|---|
| Value only needed during the run | Variable |
| List needed during the run | Array |
| Data needed on the next run | File |
Exam traps
- WRITE to add data. It erases the old data.
- No CLOSEFILE.
- Wrong order when reading.
- Wrong mode for the operation.
- File name not a string. It needs quotation marks.
- Variables confused with files. Variables are lost when the program ends.
Self-test
- Why is data in variables lost when a program ends?
- State two reasons for storing data in a file.
- Which command opens a file?
- Which mode must be used to read a file?
- What happens to existing data when a file is opened in
WRITEmode? - What does
READFILEdo? - Why should a file be closed?
- Can a file be open in both modes at once?
- How many items does one
WRITEFILEwrite? - How would you add a new line to an existing file using only
READandWRITE?
Answers
- Variables are held in RAM, which is volatile.
- Any two: data is kept after the program ends; larger amounts can be stored; data can be shared; data can be changed without changing the program.
OPENFILE.READ.- It is lost; a new file is created.
- It reads one data item from the file into a variable.
- To make sure all data is written and to release the file.
- No, a file should be opened in only one mode at a time.
- One data item (or one line of text).
- Read the existing lines into variables or an array, close the file, then open it in
WRITEmode and write the old lines followed by the new one.
Command words to expect
| Command word | What to write |
|---|---|
| State, Give, Identify | A short answer with no explanation |
| Define | A precise meaning |
| Describe | The main points or characteristics, or the steps in order |
| Explain | A reason or “how and why”, supported with evidence from the question |
| Compare | Similarities and differences, naming both sides every time |
| Suggest | A sensible proposal that applies your knowledge to the situation given |
| Evaluate | A judgement based on advantages and disadvantages, ending with a conclusion |
Read the full explanations in the File handling study guide, then try the File handling practice questions.
Official syllabus
Cambridge IGCSE Computer Science (0478) syllabus for 2026, 2027 and 2028, section 8.3: official syllabus PDF.
Related resources
-
Study guides
Cambridge IGCSE Computer Science (0478): Programming concepts
Study guide to Cambridge IGCSE Computer Science 0478 section 8.1: variables, data types, input and output, selection, iteration, totalling and counting, string handling, operators, subroutines and maintainable code in Cambridge pseudocode.
Computer Science · Cambridge · IGCSE
Read guide -
Practice questions
Cambridge IGCSE Computer Science (0478): Programming concepts – Practice Questions
Original questions with full answers on Cambridge IGCSE Computer Science 0478 section 8.1: trace tables, loops, operators, string handling, subroutines and library routines.
Computer Science · Cambridge · IGCSE
Try questions -
Revision notes
Cambridge IGCSE Computer Science (0478): Programming concepts – Revision Notes
Revision notes for Cambridge IGCSE Computer Science 0478 section 8.1: pseudocode keywords, data types, selection, loops, operators, procedures, functions and library routines.
Computer Science · Cambridge · IGCSE
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.