Variables and arrays live in main memory and vanish when the program stops. A text file on secondary storage keeps the data for next time. Learn why files are needed and the exact 9618 pseudocode to read, write and append lines of text.
Data in variables is held in RAM and is lost when the program ends. A file is stored on secondary storage, so the data persists and can be used the next time the program runs or by another program. A text file is a sequence of lines, each read or written as a string, in order. A file is opened in one mode: READ, WRITE (creates a new file; any existing data is lost) or APPEND (adds after the existing data). Use READFILE, WRITEFILE, the EOF function and CLOSEFILE.
Any data that must outlive a single run of a program has to be saved somewhere other than main memory.
| Statement | What it does |
|---|---|
OPENFILE <file> FOR READ | open for reading, from the first line |
OPENFILE <file> FOR WRITE | create a new file; any existing data in it is lost |
OPENFILE <file> FOR APPEND | add new lines after the existing data |
READFILE <file>, <variable> | read the next line into a STRING variable |
WRITEFILE <file>, <data> | write the data as a new line |
EOF(<file>) | TRUE if there are no more lines to read |
CLOSEFILE <file> | close the file when it is no longer needed |
"Files are needed to store data" is too vague. Say that variables are lost when the program ends, so the data must be stored permanently in a file to be used again later.
In the lab, run the program that writes three names to a file and then reads one back. Then imagine you do not know how many lines there are: that is what EOF is for.
DECLARE LineOfText : STRING
DECLARE Total : INTEGER
DECLARE CountA : INTEGER
Total ← 0
CountA ← 0
OPENFILE "Names.txt" FOR READ
WHILE NOT EOF("Names.txt")
READFILE "Names.txt", LineOfText
Total ← Total + 1
IF MID(LineOfText, 1, 1) = "A" THEN
CountA ← CountA + 1
ENDIF
ENDWHILE
CLOSEFILE "Names.txt"
OUTPUT CountA, " of ", Total, " names start with A"
2 of 5 names start with AREADFILE reads one line into a STRING variable. If a line holds a number, it is still text until it is converted, so do not declare the READFILE variable as INTEGER.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Why are files needed in a program?
When does EOF("Names.txt") return TRUE?
Which file mode adds new lines after the existing contents?
In the lab, three names are written to Names.txt, the file is closed and reopened FOR READ, then one READFILE is done. Which name is read?
WRITE starts a brand-new file every time it is opened; APPEND keeps what is there and adds to the end. A file is open in only one mode at a time, so close it and open it again to switch.
DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "Clean.txt" FOR WRITE
WHILE NOT EOF("FileA.txt")
READFILE "FileA.txt", LineOfText
IF LineOfText <> "" THEN
WRITEFILE "Clean.txt", LineOfText
ENDIF
ENDWHILE
CLOSEFILE "FileA.txt"
CLOSEFILE "Clean.txt"
OPENFILE "Log.txt" FOR APPEND
WRITEFILE "Log.txt", "Clean.txt created"
CLOSEFILE "Log.txt"
Always CLOSEFILE when you have finished. An examiner checks that every OPENFILE has a matching CLOSEFILE, with the same file name, after the loop rather than inside it.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
Scores.txt already holds 20 lines. A program runs OPENFILE "Scores.txt" FOR WRITE. What happens to the 20 lines?
Which is the correct structure for reading every line of Data.txt?
What data type should the variable in READFILE "Data.txt", LineIn be?
Days.txt holds the lines Mon, Tue, Wed, Thu. A program opens it FOR READ, executes READFILE "Days.txt", Day twice, then outputs Day. What is output?
This skill is now recorded on your profile. Keep the chain going.