Master Pseudocode with Pro
Get unlimited access to the AI Tutor, Code Generator, and Converters. Plus, enjoy an ad-free experience.
File Handling in Pseudocode: A Complete Guide
Normally, when a program ends, all the data stored in its variables is completely wiped out. This is because variables are stored in RAM (volatile memory). If you want to build a game that saves high scores or a system that logs user accounts, you must utilize File Handling to store data permanently (non-volatile memory).
File handling allows your code to interface directly with the file system on a computer. It involves opening files, writing data to them, reading data from them, and securely closing them when finished to avoid corruption.
The syntax for manipulating files varies wildly depending on your exam board. This guide breaks down the core concepts generically, then explicitly covers the specific methods required for AQA, OCR, and Cambridge (CIE 9618).
Table of Contents
Core Concepts: Handling Files
Regardless of syntax, dealing with files always follows a strict lifecycle: Open the file, manipulate the data, and close the file. Let's look at the theoretical steps involved.
Modes and Opening Files
Before doing anything, you must tell the operating system what you want to do with the file. You open a file in a specific Mode. The three most common modes are READ (read data only), WRITE (overwrite existing file or create new), and APPEND (add data to the end of an existing file).
Reading Data and EOF (End of File)
Reading a file usually imports data line-by-line into a string variable. Because we often don't know how long a file is, we use an indefinite loop wrapped around an EOF (End Of File) check to read until the file is empty.
Writing Data
Writing to a file takes data from memory (like a string variable) and saves it to the disk. If the file was opened in WRITE mode, the previous contents are destroyed.
Closing Files
Once operations are complete, you must close the file. Failing to do so can leave data trapped in the memory buffer, meaning it never actually saves to the disk. It also locks the file so other programs cannot use it.
Syntax Variations by Exam Board
File handling syntax diverges heavily between specifications. Some treat files as procedural targets, while others use an Object-Oriented approach with file handles.
AQA Pseudocode Standard
The AQA specification usually focuses on descriptive terminology and utilizes a file handle (assigning the opened file to a variable). It relies on uppercase keywords like OPEN, READLINE, and CLOSE.
OCR Pseudocode Standard
OCR explicitly uses an object-oriented style heavily inspired by Python. Opening a file returns a file handle object, which you then use to call methods like .readLine() or .writeLine().
Cambridge (CIE / 9618) Standard
CIE uses a strictly procedural approach. Instead of a file handle object, you pass the literal filename (or a string variable holding it) into specific command words like OPENFILE and READFILE.
Syntax Comparison Table
| Action | CIE (9618) | OCR |
|---|---|---|
| Open Read | OPENFILE "x" FOR READ | f = openRead("x") |
| Read Line | READFILE "x", var | var = f.readLine() |
| Write Line | WRITEFILE "x", data | f.writeLine(data) |
| EOF Check | EOF("x") | f.endOfFile() |
Conclusion
File handling bridges the gap between temporary execution and permanent storage. Without it, software like word processors, game saves, or databases would be entirely impossible.
While remembering whether to use openRead() or OPENFILE can be frustrating depending on your specification, the golden rules remain the same everywhere: Always declare your intent (Read vs Write), process line-by-line using an EOF check, and always close your files when finished!
Ready to practice? Try our editor.
Give our pseudocode editor a go today for free - with a built-in compiler, tools to convert pseudocode to code, and project saving, PseudoEditor makes writing pseudocode easier than ever!
Start coding now