When a program stops, every variable it used is lost. Files let a program keep data, such as a high score or a list of names, so it can be used the next time the program runs or by another program.
Variables are held in main memory and are lost when the program ends. Storing data in a file keeps it permanently in secondary storage, so it can be used again later, by this or another program. In pseudocode: OPENFILE "Names.txt" FOR READ or FOR WRITE, then READFILE to read the next line into a variable, WRITEFILE to write a value, and CLOSEFILE when finished. Opening a file FOR WRITE creates a new file: any existing data in it is lost.
In the lab, choose Write a file, then read it. The program saves three names to Names.txt; the file panel shows the data that would still be there after the program stops.
"Files save data" earns little. Say that data in variables is lost when the program ends, but data in a file is stored permanently so it can be accessed again later.
Open the file in WRITE mode, write each item with WRITEFILE, then close it. Each WRITEFILE writes one item, a single value or a whole line of text, as a new line.
DECLARE Name : STRING
DECLARE Count : INTEGER
OPENFILE "Names.txt" FOR WRITE
FOR Count ← 1 TO 3
INPUT Name
WRITEFILE "Names.txt", Name
NEXT Count
CLOSEFILE "Names.txt"The same idea in Python (for comparison only: the exam uses pseudocode)
with open("Names.txt", "w") as file: # "w" = write mode; the file is closed automatically
for count in range(3):
name = input()
file.write(name + "\n")
Opening an existing file in WRITE mode wipes it. Only one mode is used at a time, so to read and then rewrite a file, close it and open it again in the other mode.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Why does a program store data in a file?
What happens when an existing file is opened FOR WRITE?
In the lab, the names Ava, Ben and Cara are written to Names.txt. Which name does the first READFILE read back?
Which command should be used when a program has finished with a file?
Open the file in READ mode. Each READFILE reads the next line of the file into a variable, starting from the first line.
DECLARE HighScore : INTEGER
DECLARE Score : INTEGER
OPENFILE "HighScore.txt" FOR READ
READFILE "HighScore.txt", HighScore
CLOSEFILE "HighScore.txt"
INPUT Score
IF Score > HighScore
THEN
OPENFILE "HighScore.txt" FOR WRITE
WRITEFILE "HighScore.txt", Score
CLOSEFILE "HighScore.txt"
OUTPUT "New high score!"
ENDIF
The syllabus example copies one line of text from one file to another:
DECLARE LineOfText : STRING OPENFILE "FileA.txt" FOR READ OPENFILE "FileB.txt" FOR WRITE READFILE "FileA.txt", LineOfText WRITEFILE "FileB.txt", LineOfText CLOSEFILE "FileA.txt" CLOSEFILE "FileB.txt"
The same idea in Python (for comparison only: the exam uses pseudocode)
with open("HighScore.txt", "r") as file:
high_score = int(file.readline())
Always CLOSEFILE when you have finished with a file. Closing it makes sure the data is saved and lets the file be opened again, for example in the other mode.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Which statement opens Scores.txt so that data can be read from it?
What does READFILE "Data.txt", Item do?
Which order of commands correctly saves one value to a file?
A game must remember each player's settings between sessions. Why are the settings stored in a file?
This skill is now recorded on your profile. Keep the chain going.