Text files
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
A file keeps data after the program stops
- The 2026–2028 syllabus (section 8.3) stores data in a text file.
- You open the file, read or write one item, then close it.
- This course keeps the file in memory for the one run, so you can see the result.

DECLARE Best : STRING
OPENFILE "HighScore.txt" FOR WRITE
WRITEFILE "HighScore.txt", "250"
CLOSEFILE "HighScore.txt"
OPENFILE "HighScore.txt" FOR READ
READFILE "HighScore.txt", Best
CLOSEFILE "HighScore.txt"
OUTPUT "Best so far: ", Best
- The file is opened twice: once
FOR WRITEto store the line, then againFOR READto get it back.
READ and WRITE
OPENFILE "name.txt" FOR READreads what is already there.OPENFILE "name.txt" FOR WRITEstarts a new file. Anything already in it is lost.READFILEputs the next item into a variable.WRITEFILEstores a value.CLOSEFILEwhen you have finished. A file is open in only one mode at a time.
DECLARE Entry : STRING
OPENFILE "Diary.txt" FOR WRITE
WRITEFILE "Diary.txt", "Monday: rain"
CLOSEFILE "Diary.txt"
OPENFILE "Diary.txt" FOR READ
READFILE "Diary.txt", Entry
CLOSEFILE "Diary.txt"
OPENFILE "Backup.txt" FOR WRITE
WRITEFILE "Backup.txt", Entry
CLOSEFILE "Backup.txt"
OUTPUT "Backup holds: ", Entry
- Two files can be used in one program. Each has its own
OPENFILEandCLOSEFILE.
Read every line
EOF("List.txt")isTRUEwhen there is no further line to read.WHILE NOT EOF(...)reads the whole file. EachREADFILEputs the next line into aSTRING.- Close the file when you finish. A later open can use it again.
DECLARE Entry : STRING
DECLARE Count : INTEGER
OPENFILE "Diary.txt" FOR WRITE
WRITEFILE "Diary.txt", "Monday: rain"
WRITEFILE "Diary.txt", "Tuesday: sun"
WRITEFILE "Diary.txt", "Wednesday: wind"
CLOSEFILE "Diary.txt"
Count ← 0
OPENFILE "Diary.txt" FOR READ
WHILE NOT EOF("Diary.txt") DO
READFILE "Diary.txt", Entry
Count ← Count + 1
OUTPUT Count, ". ", Entry
ENDWHILE
CLOSEFILE "Diary.txt"
OUTPUT Count, " lines read"
| Pass | EOF before the read |
READFILE gets |
Count |
|---|---|---|---|
| 1 | FALSE |
Monday: rain |
1 |
| 2 | FALSE |
Tuesday: sun |
2 |
| 3 | FALSE |
Wednesday: wind |
3 |
| – | TRUE: the loop stops |
– | 3 |
Watch out
- Declare the variable before you
READFILEinto it. WRITEreplaces the file. Read it first if you still need the old text.
Common mistakes
- Open for
READorWRITE, thenREADFILEorWRITEFILE, thenCLOSEFILE. - The name of the file is a string, in double quotes.
WHILE EOF("List.txt") DOnever starts:EOFisTRUEonly at the end, so you needNOT.- One
READFILEbefore the loop reads only the first line. PutREADFILEinside the loop.
Now you try
- The tasks use
Note.txt,FileA.txt,FileB.txtandList.txt. Write, close, open again forREAD, thenOUTPUTwhat you read. - Press Check answer to test it.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write the line Hello to Note.txt, read it back into LineOfText, and output it.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Store Saved in FileA.txt, read it into LineOfText, write that same text to FileB.txt, and output it.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write Cat and Dog to List.txt, then output both lines with WHILE NOT EOF.
Click Run to see the output here.