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.
Two flavours of writing share the same WRITEFILE keyword but differ in how the file is opened.
Used when starting fresh, or when overwriting an existing file:
OPENFILE "report.txt" FOR WRITE
WRITEFILE "report.txt", "Sales Report 2024"
WRITEFILE "report.txt", "Total: £125,000"
WRITEFILE "report.txt", "Customers: 1,432"
CLOSEFILE "report.txt"
After this runs, report.txt contains exactly those three lines. Any previous content is gone.
Used to add new records without losing existing data:
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "2024-05-17 10:42: user logged in"
WRITEFILE "log.txt", "2024-05-17 10:43: file uploaded"
CLOSEFILE "log.txt"
Each call to WRITEFILE adds a single line at the end of the file.
Example — Add a new employee Polly, who works in Sales with salary 26000 and age 32, to the existing employees.txt file.
OPENFILE "employees.txt" FOR APPEND
WRITEFILE "employees.txt", "Polly"
WRITEFILE "employees.txt", "Sales"
WRITEFILE "employees.txt", "26000"
WRITEFILE "employees.txt", "32"
CLOSEFILE "employees.txt"
After this runs, the file contains the original employee records plus the new four-line block for Polly at the end.