Why programs store data in a file

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

Why programs store data in a file

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program stores a player's score in a variable. The program ends. What happens to the score?
A It is lost, because variables only exist while the program runs
B It is kept automatically, ready for the next time the program runs
2. Which is the best way to keep a high score so it is still there tomorrow?
A Store the score in a variable
B Write the score to a file
3. Which statement about file data is correct?
A It still exists after the program closes
B It disappears as soon as the program closes
4. A quiz program runs a second time. How can it get the high score saved last time?
A Read the score back from the file
B Read the score from the old variable
5. A teacher wants a program to remember a list of student names between lessons. What should the program use?
A A variable that holds the names
B A file that stores the names

Match the Pairs

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

Left columnRight column
1OPENFILE "HighScore.txt" FOR WRITE AOpens a file on storage so the program can save its data there, and the data stays after the program ends
2score ← 250 BStores a value in a variable, which is lost when the program ends
3A program saves a list of names to a file so they are still there the next time it runs CA file is not deleted when the program closes. It stays on storage, so the data can be read again later.
4A program reads the saved high score back from storage when it starts again DWRITEFILE "Names.txt", Name
5Score ← 250 // The program ends. Next run, the score is still 250 because variables keep their values. EREADFILE "HighScore.txt", Score
6// A file is deleted from storage when the program closes, so the data is gone. FVariables do not keep their values after the program ends. The score must be saved in a file to last.

Definitions

file

Fill in the Blanks

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

1.
OPENFILE "HighScore.txt" FOR _____
2.
OPENFILE "HighScore.txt" FOR WRITE _________ "HighScore.txt", Score CLOSEFILE "HighScore.txt"
3.
OPENFILE "HighScore.txt" FOR READ READFILE "HighScore.txt", _____ CLOSEFILE "HighScore.txt"
4.
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name _________ "Names.txt"
5.
// Next run: get the saved names back OPENFILE "Names.txt" FOR ____

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A DECLARE Score : INTEGER
B Score ← 250
C OPENFILE "score.txt" FOR WRITE
D READFILE "score.txt", Score
E CLOSEFILE "score.txt"
Question 2
A OPENFILE "names.txt" FOR READ
B READFILE "names.txt" Name
C OUTPUT Name
D CLOSEFILE "names.txt"
Question 3
A OPENFILE "best.txt" FOR WRITE
B WRITEFILE best.txt, Best
C CLOSEFILE "best.txt"
Question 4
A OPENFILE "best.txt" FOR WRITE
B READFILE "best.txt", Best
C OUTPUT "High score: ", Best
D CLOSEFILE "best.txt"
Question 5
A DECLARE Names : STRING
B OPENFILE "names.txt" FOR READ
C READFILE Names, "names.txt"
D OUTPUT Names

Answer Key

Why programs store data in a file
Section A: Quick Questions
Q1
A — It is lost, because variables only exist while the program runs
Q2
B — Write the score to a file
Q3
A — It still exists after the program closes
Q4
A — Read the score back from the file
Q5
B — A file that stores the names
Section B: Match the Pairs
Q1
OPENFILE "HighScore.txt" FOR WRITE → Opens a file on storage so the program can save its data there, and the data stays after the program ends
Q2
score ← 250 → Stores a value in a variable, which is lost when the program ends
Q3
A program saves a list of names to a file so they are still there the next time it runs → WRITEFILE "Names.txt", Name
Q4
A program reads the saved high score back from storage when it starts again → READFILE "HighScore.txt", Score
Q5
Score ← 250 // The program ends. Next run, the score is still 250 because variables keep their values. → Variables do not keep their values after the program ends. The score must be saved in a file to last.
Q6
// A file is deleted from storage when the program closes, so the data is gone. → A file is not deleted when the program closes. It stays on storage, so the data can be read again later.
Section C: Definitions
Q1
file — A file keeps data on storage so it still exists after the program closes, unlike variables. The program can read it back next time it runs, such as a saved high score or a list of names.
Section D: Fill in the Blanks
Q1
OPENFILE "HighScore.txt" FOR WRITE
Q2
OPENFILE "HighScore.txt" FOR WRITE WRITEFILE "HighScore.txt", Score CLOSEFILE "HighScore.txt"
Q3
OPENFILE "HighScore.txt" FOR READ READFILE "HighScore.txt", Score CLOSEFILE "HighScore.txt"
Q4
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
Q5
// Next run: get the saved names back OPENFILE "Names.txt" FOR READ
Section E: Spot the Error
Q1
D — The file is open for WRITE and the aim is to save the score so it survives after the program ends. The line should be WRITEFILE "score.txt", Score, not READFILE.
Q2
B — READFILE needs a comma between the file name and the variable that will store the saved name. It should be READFILE "names.txt", Name.
Q3
B — The file name is a string, so it must be in quotes. Without them the program cannot find the file that stores the high score. It should be WRITEFILE "best.txt", Best.
Q4
A — To read the saved high score back next time the program runs, the file must be opened FOR READ. WRITE mode would replace the old data. It should be OPENFILE "best.txt" FOR READ.
Q5
C — The file name must come first and the variable second. It should be READFILE "names.txt", Names so the saved data is read into the variable.