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 just two file modes: READ and WRITE.
| 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 |
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 existing data, read the file in first, then write the old and new content back out.
| Goal | Use mode |
|---|---|
| Load existing data into the program | READ |
| Create a fresh new file (or replace an old one entirely) | WRITE |