Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 3% of your exam marks.
OPENFILE, READFILE, WRITEFILE, CLOSEFILE pseudocode operations appear occasionally.
A file must be opened in a mode that tells the operating system what the program intends to do with it. The mode appears after FOR in the OPENFILE statement. CIE pseudocode has three file modes: READ, WRITE and .
| Mode | What it does | What happens to existing contents |
|---|---|---|
READ | Opens the file for reading only | Untouched; the file must already exist |
WRITE | Opens the file for writing new content | Erased: if the file already exists, its contents are overwritten |
APPEND |
Used when the program needs to load data from a . The file must already exist; opening a non-existent file in READ mode causes an error.
OPENFILE "students.txt" FOR READ
READFILE "students.txt", Name
OUTPUT Name
CLOSEFILE "students.txt"
Used when the program is creating a new file or completely replacing an existing one. If the file already exists, everything in it is wiped out before the new content is written.
OPENFILE "report.txt" FOR WRITE
WRITEFILE "report.txt", "Sales Report 2024"
WRITEFILE "report.txt", "Total: £125,000"
CLOSEFILE "report.txt"
Important: opening a file that already has data in
WRITEmode destroys its existing contents before writing. To keep the existing data and add to it, use APPEND mode instead.
Used when the program needs to add new content to the end of a file without erasing what is already there. If the file does not yet exist, it is created.
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry added at the end"
CLOSEFILE "log.txt"
| Goal | Use mode |
|---|---|
| Load existing data into the program | READ |
| Create a fresh new file (or replace an old one entirely) | WRITE |
| Add a new record to the end of an existing file | APPEND |
| Opens the file to add to the end |
| Kept: new data is added after the existing content |