Write, close, then read the data back

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Write, close, then read the data back

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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"
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
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
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"
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1CLOSEFILE("Scores.txt") AFinishes writing and saves the file, so it can be reopened in READ mode to get the data back
2OPENFILE "Scores.txt" FOR READ BCLOSEFILE is missing. The file must be closed after writing before it is reopened in READ mode.
3Data is saved by opening the file for WRITE, writing the item, then closing the file COPENFILE "Name.txt" FOR WRITE WRITEFILE "Name.txt", Name CLOSEFILE("Name.txt")
4After reopening the file for READ, READFILE gets back the stored data and OUTPUT shows it was kept DThe file name in the READ statement is different from the one written, so the saved data is not found.
5OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score OPENFILE "Data.txt" FOR READ EOPENFILE "Name.txt" FOR READ READFILE "Name.txt", Saved OUTPUT Saved
6OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score CLOSEFILE("Data.txt") OPENFILE "Score.txt" FOR READ FReopens the file that was written earlier, using the same file name, so its data can be read back

Definitions

WRITE, CLOSEFILE, READ

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
OPENFILE "score.txt" FOR WRITE WRITEFILE "score.txt", Points _________ "score.txt"
2.
CLOSEFILE "names.txt" OPENFILE "_________" FOR READ READFILE "names.txt", Name
3.
OPENFILE "age.txt" FOR READ ________ "age.txt", Age OUTPUT Age
4.
CLOSEFILE "age.txt" OPENFILE "age.txt" FOR ____ READFILE "age.txt", Age
5.
OPENFILE "age.txt" FOR READ READFILE "age.txt", Age ______ Age

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
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

Answer Key

Write, close, then read the data back
Section A: Quick Questions
Q1
B — CLOSEFILE "scores.txt"
Q2
A — OPENFILE FOR WRITE, WRITEFILE, CLOSEFILE, OPENFILE FOR READ, READFILE
Q3
A — OPENFILE "pets.txt" FOR WRITE ... OPENFILE "pets.txt" FOR READ
Q4
B — "Mia"
Q5
A — It shows the data that was kept in the file
Section B: Match the Pairs
Q1
CLOSEFILE("Scores.txt") → Finishes writing and saves the file, so it can be reopened in READ mode to get the data back
Q2
OPENFILE "Scores.txt" FOR READ → Reopens the file that was written earlier, using the same file name, so its data can be read back
Q3
Data is saved by opening the file for WRITE, writing the item, then closing the file → OPENFILE "Name.txt" FOR WRITE WRITEFILE "Name.txt", Name CLOSEFILE("Name.txt")
Q4
After reopening the file for READ, READFILE gets back the stored data and OUTPUT shows it was kept → OPENFILE "Name.txt" FOR READ READFILE "Name.txt", Saved OUTPUT Saved
Q5
OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score OPENFILE "Data.txt" FOR READ → CLOSEFILE is missing. The file must be closed after writing before it is reopened in READ mode.
Q6
OPENFILE "Data.txt" FOR WRITE WRITEFILE "Data.txt", Score CLOSEFILE("Data.txt") OPENFILE "Score.txt" FOR READ → The file name in the READ statement is different from the one written, so the saved data is not found.
Section C: Definitions
Q1
WRITE, CLOSEFILE, READ — Open a file for WRITE, store data with WRITEFILE, then CLOSEFILE. Reopen the same file name for READ and READFILE gets the data back to use again.
Section D: Fill in the Blanks
Q1
OPENFILE "score.txt" FOR WRITE WRITEFILE "score.txt", Points CLOSEFILE "score.txt"
Q2
CLOSEFILE "names.txt" OPENFILE "names.txt" FOR READ READFILE "names.txt", Name
Q3
OPENFILE "age.txt" FOR READ READFILE "age.txt", Age OUTPUT Age
Q4
CLOSEFILE "age.txt" OPENFILE "age.txt" FOR READ READFILE "age.txt", Age
Q5
OPENFILE "age.txt" FOR READ READFILE "age.txt", Age OUTPUT Age
Section E: Spot the Error
Q1
B — Line 2 must store the data in the file, so it needs WRITEFILE, not READFILE. It should be WRITEFILE "data.txt", Score.
Q2
A — The program writes to the file next, so it must be opened for WRITE first. It should be OPENFILE "name.txt" FOR WRITE.
Q3
D — The file identifier must be the same in both OPENFILE statements. "marks.txt" is a different file, so it should be "mark.txt".
Q4
B — WRITEFILE needs the file identifier first, then the data to write. It should be WRITEFILE "city.txt", City.
Q5
D — The keyword FOR is missing before the mode. It should be OPENFILE "age.txt" FOR READ.