Pseudocode reference
Reading and writing files
Open, read, write, and close text files in Cambridge-style pseudocode.
updated November 2, 2026
Four steps
Every file operation follows the same pattern.
- 1DeclareThe file name and data variables
- 2Open
OPENFILEwithFOR READorFOR WRITE - 3Read or write
READFILEorWRITEFILE - 4Close
CLOSEFILEwhen you finish
Read a line
READFILE reads the next line of the file into a string variable.
Pseudocode
DECLARE FileName : STRING
DECLARE LineText : STRING
FileName <- "Scores.txt"
OPENFILE FileName FOR READ
READFILE FileName, LineText
OUTPUT "First line: ", LineText
CLOSEFILE FileNameWrite a line
WRITEFILE writes a line to a file opened FOR WRITE.
Pseudocode
DECLARE FileName : STRING
FileName <- "Scores.txt"
OPENFILE FileName FOR WRITE
WRITEFILE FileName, "Mai,42"
CLOSEFILE FileNameWatch out. Don't open the same file for READ and WRITE at the same time, and always close a file, even in a short algorithm.