Course 8.3Lesson 3 of 3
Variables and arrays are lost when a program stops. This lesson explains why programs store data in files, and how Cambridge pseudocode opens, reads, writes and closes a text file, beside the same steps in Python.
Try the code and the simulations beside the lesson.
A game shows the high score when you start it again tomorrow. Where was that score while the computer was off? Not in a variable. Variables and arrays live in main memory, and they are lost when the program stops. A file is kept on storage. So a program stores data in a file when it must be kept after the program ends, or used later by this program or by another one. A file can also bring in a large amount of data that nobody wants to type again.
The Python Going Further book on this site has a whole chapter, "Files and Data Formats". The syllabus needs only the simple part: text files, read and written one item or one line at a time.
OPENFILE <name> FOR READ opens a file so that data can be read from it.OPENFILE <name> FOR WRITE opens a file for writing. A new file is created, and any data already in that file is lost.READFILE <name>, <variable> reads the next item, a line of the file, into the variable.WRITEFILE <name>, <value> writes a value into the file, as a new line.CLOSEFILE <name> closes the file when it is no longer needed.Open a file in one mode at a time. Always close it. The file name is a string, such as "Scores.txt".
This program saves a high score:
DECLARE PlayerName : STRING
DECLARE HighScore : INTEGER
PlayerName ← "Amira"
HighScore ← 87
OPENFILE "Scores.txt" FOR WRITE
WRITEFILE "Scores.txt", PlayerName
WRITEFILE "Scores.txt", HighScore
CLOSEFILE "Scores.txt"The file Scores.txt now holds two lines: Amira and 87. Later, a second program loads them:
DECLARE BestName : STRING
DECLARE BestScore : INTEGER
OPENFILE "Scores.txt" FOR READ
READFILE "Scores.txt", BestName
READFILE "Scores.txt", BestScore
CLOSEFILE "Scores.txt"
OUTPUT BestName, " has the high score of ", BestScoreThe items are read in the order they were written. The output is Amira has the high score of 87.
The picture shows the journey. WRITEFILE copies values from variables in memory into the file on storage. READFILE copies them back into variables, line by line.
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"To read several values, put READFILE in a loop. The program must know how many lines there are, because the syllabus has no command to test for the end of a file. Here five temperatures are read into an array:
DECLARE Temps : ARRAY[1:5] OF INTEGER
DECLARE Index : INTEGER
OPENFILE "Temps.txt" FOR READ
FOR Index ← 1 TO 5
READFILE "Temps.txt", Temps[Index]
NEXT Index
CLOSEFILE "Temps.txt"If Temps.txt holds 18, 21, 19, 24 and 22, the array holds them in the same order. Temps[4] is 24.
The program writes a file, closes it and reads it back, then shows what happens when a file is opened for writing a second time. Python's "w" is WRITE and "r" is READ.
OPENFILE ... FOR READ or FOR WRITE, then READFILE or WRITEFILE, then CLOSEFILE.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Marks.txt so that the program can read it.Temps[2]?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.