In simple terms
A friendly intro before the formal notes — no formulas yet.
Your Programme's Long-Term Memory
Variables store data while a programme is running, but what happens when it closes? Files act as a permanent storage, like a digital filing cabinet, allowing your programme to save its work and retrieve it later.
Imagine you're working in an office. Your desk is like your computer's RAM – you can work with papers (data) on it, but at the end of the day, it gets cleared. To save your work, you put the papers into a specific folder in a filing cabinet. This is like writing to a file. The next day, you retrieve that folder to continue. This is like reading from a file. You must open the cabinet drawer first (open file) and close it when you're done (close file).
- 1
Choose your action: Decide if you need to read existing data, create a new file, or add to an existing one. This determines the 'mode' (READ, WRITE, APPEND).
- 2
Open the file: Use the OPENFILE command with the filename and your chosen mode. This gives your program access.
- 3
Process the data: Use a loop with READFILE to get data from the file, or use WRITEFILE to save data to it.
- 4
Close the file: Use the CLOSEFILE command. This is crucial to save changes and release the file for other programs to use.
Explore the concept
Use the live diagram and synced steps — play it or tap a step card to walk through.
Full topic notes
Formal explanation with the rigour you need for the exam.
File Organisation: Records and Fields
In A-Level Computer Science, we primarily deal with text files. These are simple files containing lines of text. To structure the data within them, we often think in terms of records and fields. A record is a complete set of information about a single entity, like a person or an item. Each line in a file often represents one record. A field is a single piece of information within that record. Fields within a record are typically separated by a delimiter, such as a comma.
File: A named collection of data stored on secondary storage.
Record: A line or structured block of data representing one item (e.g., '101,Bob,17').
Field: A single data item within a record (e.g., 'Bob').
Core File Handling Commands
Cambridge International exam pseudocode provides four fundamental commands for file I/O (Input/Output). Your first step is always to open the file, specifying how you intend to use it. This 'how' is called the mode.
OPENFILE <filename> FOR <mode> // Modes are READ, WRITE, APPEND READFILE <filename>, <VariableName> // Reads one line into a variable WRITEFILE <filename>, <Data> // Writes data (e.g., a variable's content) as a new line CLOSEFILE <filename> // Saves changes and releases the file
READ: For input only. The file must already exist.
WRITE: For output only. Creates a new file, deleting any existing file with the same name.
APPEND: For output only. Adds data to the end of an existing file or creates a new one if it doesn't exist.
In an exam, you will almost always get a mark for opening a file in the correct mode and for closing it at the end. Don't forget CLOSEFILE – it's an easy mark to lose! Forgetting to close a file after writing might mean the data is never actually saved.
Reading Data from a File
To read the contents of a file, you must first open it in READ mode. The most common pattern is to use a WHILE loop that continues as long as you have not reached the end of the file. The EOF() function is perfect for this; it returns FALSE as long as there is more data to read, and TRUE once the end is reached.
OPENFILE "Students.txt" FOR READ WHILE NOT EOF("Students.txt") READFILE "Students.txt", StudentName // Process the StudentName variable here ENDWHILE CLOSEFILE "Students.txt"
Writing and Appending Data
To save data, you open a file in either WRITE or APPEND mode. The choice is critical. Use WRITE when you want to create a brand new file or completely replace an old one. This is destructive. Use APPEND when you want to add new information to the end of an existing file without deleting what's already there. This is non-destructive.
Worked examples
See the formulas applied — reveal one step at a time, like the exam.
A text file, Scores.txt, contains a list of scores, one per line. Write pseudocode to read all the scores from the file, calculate their total, and output the final total.
File Content (Scores.txt):
150
230
95
- 1
// 1. Initialise variables DECLARE TotalScore : INTEGER DECLARE CurrentScore : INTEGER TotalScore <- 0
A programme needs to save a new user's username to a file called Users.txt. The username is stored in a variable NewUser. The programme should add this new user to the end of the list without deleting existing users. Write the pseudocode to accomplish this.
Initial File Content (Users.txt):
Alice
Bob
Variable Value:
- 1
DECLARE NewUser : STRING NewUser <- "Charlie"
How it all connects
The big idea sits in the middle — tap a linked idea to explore the link.
Tap a linked idea to see how it connects back to the main topic — that connection is what examiners reward.
Glossary
Try to recall each definition before you reveal it.
Quick check
Answer in your head first — then tap to check. No pressure.
Revision flashcards
Flip the card. Test yourself before the exam.
What is a text file?
A type of file that stores data as a sequence of characters, organised into lines. It can be opened and read by a human using a simple text editor.
Key takeaways
Review these before you close the topic — retrieval beats re-reading.
- ✓
File: A named collection of data stored on secondary storage.
- ✓
Record: A line or structured block of data representing one item (e.g., '101,Bob,17').
- ✓
Field: A single data item within a record (e.g., 'Bob').
Practice — then mark it
The whole point: a real Cambridge question, marked mark-by-mark.
Test Your File Handling Skills
Test Your File Handling Skills
Extra simulations & links
PhET, GeoGebra and other curated tools — open in a new tab.
Frequently asked
Checkpoint
One marked question is worth ten re-reads — close the loop before you move on.
Reading it isn’t knowing it — prove it.
Before you move on: do Test Your File Handling Skills on paper, snap a photo, and get examiner-style feedback on exactly where you win and lose marks.