Understanding Write, close, then read the data back in IGCSE Computer Science Paper 2
What it does: This is the saving stage: OPENFILE for WRITE, WRITEFILE to store the data, then CLOSEFILE to finish. READFILE puts the stored data into the variable Saved, and OUTPUT displays it to prove the data was kept. The file must be closed after writing. Then it can be reopened in READ mode so the saved data can be read back.
Common mistakes: Line 2 must store the data in the file, so it needs WRITEFILE, not READFILE. It should be WRITEFILE "data.txt", Score. The program writes to the file next, so it must be opened for WRITE first. It should be OPENFILE "name.txt" FOR WRITE.
Quick Questions
1. A program has written a value to "scores.txt" using WRITEFILE. What must happen before it reopens the file for READ?
A. OUTPUT "scores.txt"
B. CLOSEFILE "scores.txt" ✓
The file must be closed after writing. Then it can be reopened in READ mode so the saved data can be read back.
2. Which order saves data and then uses it again?
A. OPENFILE FOR WRITE, WRITEFILE, CLOSEFILE, OPENFILE FOR READ, READFILE ✓
B. OPENFILE FOR READ, READFILE, OPENFILE FOR WRITE, WRITEFILE, CLOSEFILE
Data must be written and the file closed before the file is opened for READ. Reading first means there is nothing saved to read yet.
3. Which pair of statements uses the same file correctly in both modes?
A. OPENFILE "pets.txt" FOR WRITE ... OPENFILE "pets.txt" FOR READ ✓
B. OPENFILE "pets.txt" FOR WRITE ... OPENFILE "animals.txt" FOR READ
The file identifier must be identical in both OPENFILE statements. A different name opens a different file, so the saved data is not found.
4. A program runs WRITEFILE "Mia" in WRITE mode, then CLOSEFILE, then reopens the same file for READ and runs READFILE Name. What does Name hold?
A. Nothing, because the data is lost when the file is closed
B. "Mia" ✓
Closing a file does not delete the data. It is kept in the file, and READFILE gets it back.
5. What is the purpose of OUTPUT Name after READFILE Name?
A. It shows the data that was kept in the file ✓
B. It saves the data into the file
OUTPUT displays a value on the screen. Saving to a file is done with WRITEFILE, so OUTPUT is used to show the data was read back.
Fill in the Blanks
1. OPENFILE "score.txt" FOR WRITE
WRITEFILE "score.txt", Points
CLOSEFILE "score.txt"
Hint: Type the command that finishes with the file after writing, before it is reopened in the other mode.
2. CLOSEFILE "names.txt"
OPENFILE "names.txt" FOR READ
READFILE "names.txt", Name
Hint: Type the file name that must match the one used when the data was written. Look at the line above.
3. OPENFILE "age.txt" FOR READ
READFILE "age.txt", Age
OUTPUT Age
Hint: Type the command that gets the stored data back from the file into the variable.
4. CLOSEFILE "age.txt"
OPENFILE "age.txt" FOR READ
READFILE "age.txt", Age
Hint: Type the file mode needed to get the saved data back out of the file.
5. OPENFILE "age.txt" FOR READ
READFILE "age.txt", Age
OUTPUT Age
Hint: Type the keyword that displays the value on screen to show the data was kept.
Spot the Error
Question 1
A
OPENFILE "data.txt" FOR WRITE
B
READFILE "data.txt", Score
C
CLOSEFILE "data.txt"
D
OPENFILE "data.txt" FOR READ
E
READFILE "data.txt", Back
F
OUTPUT Back
Line B — Line 2 must store the data in the file, so it needs WRITEFILE, not READFILE. It should be WRITEFILE "data.txt", Score.
Question 2
A
OPENFILE "name.txt" FOR READ
B
WRITEFILE "name.txt", Name
C
CLOSEFILE "name.txt"
D
OPENFILE "name.txt" FOR READ
E
READFILE "name.txt", Saved
F
OUTPUT Saved
Line A — The program writes to the file next, so it must be opened for WRITE first. It should be OPENFILE "name.txt" FOR WRITE.
Question 3
A
OPENFILE "mark.txt" FOR WRITE
B
WRITEFILE "mark.txt", Mark
C
CLOSEFILE "mark.txt"
D
OPENFILE "marks.txt" FOR READ
E
READFILE "mark.txt", Result
F
OUTPUT Result
Line D — The file identifier must be the same in both OPENFILE statements. "marks.txt" is a different file, so it should be "mark.txt".
Question 4
A
OPENFILE "city.txt" FOR WRITE
B
WRITEFILE City, "city.txt"
C
CLOSEFILE "city.txt"
D
OPENFILE "city.txt" FOR READ
E
READFILE "city.txt", Town
F
OUTPUT Town
Line B — WRITEFILE needs the file identifier first, then the data to write. It should be WRITEFILE "city.txt", City.
Question 5
A
OPENFILE "age.txt" FOR WRITE
B
WRITEFILE "age.txt", Age
C
CLOSEFILE "age.txt"
D
OPENFILE "age.txt" READ
E
READFILE "age.txt", NewAge
F
OUTPUT NewAge
Line D — The keyword FOR is missing before the mode. It should be OPENFILE "age.txt" FOR READ.