Revision Notes
Cambridge O Level Computer Science (2210): File handling – Revision Notes
Revision notes for Cambridge O Level Computer Science 2210 section 8.3: why files are used and the OPENFILE, READFILE, WRITEFILE and CLOSEFILE commands, with patterns and a self-test.
- Subject
- Computer Science
- Level
- O LEVELS
- Topic
- Topic 8 – Programming
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to Cambridge O Level Computer Science (2210), 2026-2028. Official specification .
Syllabus page (what it covers and how it is assessed): Cambridge O Level Computer Science.
Syllabus points this page covers
2210
- 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 O Level Computer Science (2210).
Condensed notes for syllabus section 8.3, File handling. For the full explanations and worked examples, use the File handling study guide.
Syllabus checklist
- The purpose of storing data in a file to be used by a program.
- Open, close and use a file for reading and writing.
- Read and write single items of data.
- Read and write a line of text.
Key terms
| Term | Definition |
|---|---|
| File | A named collection of data stored on secondary storage |
| Volatile | Loses its contents when the power is off or the program ends (RAM) |
| Open | Make a file ready for use, stating the mode |
| Close | Finish with the file so that the data is saved and the file is released |
| READ mode | The file is opened so that data can be read from it |
| WRITE mode | The file is opened so that data can be written to it; a new file is created and old data is lost |
| Line of text | A string stored as one item on a line of the file |
Why use files
- Variables and arrays are in RAM and their contents are lost when the program stops.
- A file is on secondary storage, so the data stays.
- Data can be kept between runs, such as a high score.
- Large amounts of data can be stored outside the program.
- Data can be shared with other programs or users.
- Data prepared outside the program can be read in.
The four commands
| Command | Form | Example |
|---|---|---|
| Open | OPENFILE <name> FOR <mode> |
OPENFILE "Scores.txt" FOR READ |
| Read | READFILE <name>, <variable> |
READFILE "Scores.txt", Score |
| Write | WRITEFILE <name>, <variable> |
WRITEFILE "Scores.txt", Score |
| Close | CLOSEFILE <name> |
CLOSEFILE "Scores.txt" |
Rules:
- One mode at a time for each file.
- WRITE replaces existing data (the file is newly created).
- READ needs a file that exists.
- Close the file when it is no longer needed.
- To change mode, close the file and open it again.
Order of statements
DECLAREthe variables.OPENFILEwith the mode.- A loop (or single statement) with
READFILEorWRITEFILE. CLOSEFILE.
Pattern 1: write several items
OPENFILE "Names.txt" FOR WRITE
FOR Count ← 1 TO 4
INPUT Name
WRITEFILE "Names.txt", Name
NEXT Count
CLOSEFILE "Names.txt"
Pattern 2: read and total
Total ← 0
OPENFILE "Prices.txt" FOR READ
FOR Count ← 1 TO 6
READFILE "Prices.txt", Price
Total ← Total + Price
NEXT Count
CLOSEFILE "Prices.txt"
OUTPUT Total
Pattern 3: read a line of text
OPENFILE "Notes.txt" FOR READ
READFILE "Notes.txt", Line
CLOSEFILE "Notes.txt"
OUTPUT Line
Pattern 4: copy between files
OPENFILE "A.txt" FOR READ
OPENFILE "B.txt" FOR WRITE
READFILE "A.txt", Line
WRITEFILE "B.txt", Line
CLOSEFILE "A.txt"
CLOSEFILE "B.txt"
Pattern 5: update a stored value
- Open for READ, read the old value, CLOSE.
- Compare with the new value.
- If the new value is better, open for WRITE, write it, CLOSE.
Trace practice
File Marks.txt holds 8, 3, 9 (one per line).
Best ← 0
OPENFILE "Marks.txt" FOR READ
FOR Count ← 1 TO 3
READFILE "Marks.txt", Mark
IF Mark > Best
THEN
Best ← Mark
ENDIF
NEXT Count
CLOSEFILE "Marks.txt"
OUTPUT Best
| Count | Mark | Best |
|---|---|---|
| 1 | 8 | 8 |
| 2 | 3 | 8 |
| 3 | 9 | 9 |
The output is 9.
About the end of a file
The syllabus pseudocode lists the four commands above and no end-of-file test. Questions therefore give the number of items, or use a stop value. Use a FOR loop with that number.
Answer templates
Explain why a program stores data in a file (2 marks): RAM is volatile so variables lose their contents when the program ends; a file on secondary storage keeps the data so that it can be used again.
Write pseudocode to save data in a file (4 or 5 marks): OPENFILE with the name and WRITE; loop if there are several items; WRITEFILE with the name and variable; CLOSEFILE.
Write pseudocode to read from a file (4 or 5 marks): OPENFILE with READ; READFILE into a variable of the right type; use the value; CLOSEFILE.
Exam traps
- No CLOSEFILE. It is usually a mark.
- WRITE mode used to read.
- Old data lost by using WRITE on a file that needs to keep its contents.
- Missing the name in READFILE or WRITEFILE.
- Open or close inside the loop for no reason.
- Using the wrong variable type for data read from the file.
Self-test
- Why is a file better than a variable for storing a high score?
- Which mode is used to read a file?
- What happens to existing data when a file is opened for WRITE?
- Write the statement that opens “Data.txt” for reading.
- Write the statement that reads the next item into Value.
- Write the statement that writes Total into “Out.txt”.
- Why should a file be closed after use?
- What must you do before reading a file that you have just written?
- Give two reasons for storing data in a file.
- How is a line of text stored in a file?
- Why do exam questions give the number of items in a file?
- If a file holds 4, 6 and 10 and the program totals them, what is the total?
Answers
- A variable is in RAM and is lost when the program ends; a file is stored on secondary storage and keeps its data.
- READ.
- A new file is created and the existing data is lost.
OPENFILE "Data.txt" FOR READREADFILE "Data.txt", ValueWRITEFILE "Out.txt", Total- So that all the data is saved and the file is released for other use.
- Close it, then open it again in READ mode.
- Any two: keep data between runs; store large amounts; share data; read data prepared elsewhere.
- As a string, written or read as one item.
- The pseudocode has no end-of-file test, so the loop needs a known count.
-
Related resources
-
Study guides
Cambridge O Level Computer Science (2210): Programming concepts
Study guide to Cambridge O Level Computer Science 2210 section 8.1: variables, data types, selection, iteration, operators, procedures and functions, library routines and maintainable code, in Cambridge pseudocode.
Computer Science · Cambridge · O Level
Read guide -
Practice questions
Cambridge O Level Computer Science (2210): Programming concepts – Practice Questions
Original questions with answers on Cambridge O Level Computer Science 2210 section 8.1: data types, operators, selection, loops, procedures, functions and maintainable pseudocode.
Computer Science · Cambridge · O Level
Try questions -
Revision notes
Cambridge O Level Computer Science (2210): Programming concepts – Revision Notes
Revision notes for Cambridge O Level Computer Science 2210 section 8.1: pseudocode syntax for variables, selection, loops, operators, string handling, procedures, functions and library routines, with a self-test.
Computer Science · Cambridge · O Level
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.