OPENFILE ... FOR READ

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

OPENFILE ... FOR READ

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line opens the existing file Scores.txt so a program can read from it?
A OPENFILE "Scores.txt" FOR READ
B OPENFILE "Scores.txt" FOR WRITE
2. True or false: A file must already exist before you can use OPENFILE ... FOR READ on it.
A False
B True
3. Which order is correct for reading a line from a file?
A OPENFILE "Scores.txt" FOR READ, then READFILE "Scores.txt", Line
B READFILE "Scores.txt", Line, then OPENFILE "Scores.txt" FOR READ
4. What happens to the data stored in Scores.txt when you run OPENFILE "Scores.txt" FOR READ?
A The old data is erased
B The data stays exactly the same
5. Which statement is correct about the file identifier in OPENFILE "Scores.txt" FOR READ?
A "Scores.txt" is the file name, written as a string
B Scores.txt is a variable, so it has no quote marks

Match the Pairs

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

Left columnRight column
1OPENFILE "Scores.txt" FOR READ AOPENFILE "Results.txt" FOR READ
2OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name BREADFILE is used before the file is opened. OPENFILE ... FOR READ must come first.
3A file must be opened in READ mode before any data is read from it COpens Names.txt first, then reads one line from it into the variable Name
4READ mode opens a file that already exists and holds data, so it is not used to create a new file DOpens the existing file Scores.txt so the program can read data from it
5OPENFILE Scores.txt FOR READ EOPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
6READFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR READ FThe file name is not in quotation marks. The file identifier must be a string, so it should be "Scores.txt".

Definitions

OPENFILE "Scores.txt" FOR READ

Fill in the Blanks

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

1.
________ "Scores.txt" FOR READ
2.
OPENFILE "Scores.txt" ___ READ
3.
OPENFILE "Scores.txt" FOR ____
4.
OPENFILE "Scores.txt" FOR READ ________ "Scores.txt", Score
5.
DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", _____

Spot the Error

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

Question 1
A OPENFILE "Scores.txt" FOR WRITE
B READFILE "Scores.txt", Score
C OUTPUT Score
D CLOSEFILE "Scores.txt"
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" READ
C READFILE "Scores.txt", Score
D OUTPUT Score
E CLOSEFILE "Scores.txt"
Question 3
A DECLARE Mark : INTEGER
B OUTPUT "Reading marks"
C OPENFILE "Marks.txt" FOR READ
D READFILE "Marks.txt" Mark
E OUTPUT Mark
Question 4
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" FOR READ
C WRITEFILE "Scores.txt", Score
D OUTPUT Score
Question 5
A DECLARE Total : INTEGER
B OPENFILE Scores.txt FOR READ
C READFILE "Scores.txt", Total
D OUTPUT Total

Answer Key

OPENFILE ... FOR READ
Section A: Quick Questions
Q1
A — OPENFILE "Scores.txt" FOR READ
Q2
B — True
Q3
A — OPENFILE "Scores.txt" FOR READ, then READFILE "Scores.txt", Line
Q4
B — The data stays exactly the same
Q5
A — "Scores.txt" is the file name, written as a string
Section B: Match the Pairs
Q1
OPENFILE "Scores.txt" FOR READ → Opens the existing file Scores.txt so the program can read data from it
Q2
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name → Opens Names.txt first, then reads one line from it into the variable Name
Q3
A file must be opened in READ mode before any data is read from it → OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q4
READ mode opens a file that already exists and holds data, so it is not used to create a new file → OPENFILE "Results.txt" FOR READ
Q5
OPENFILE Scores.txt FOR READ → The file name is not in quotation marks. The file identifier must be a string, so it should be "Scores.txt".
Q6
READFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR READ → READFILE is used before the file is opened. OPENFILE ... FOR READ must come first.
Section C: Definitions
Q1
OPENFILE "Scores.txt" FOR READ — Opens the existing file Scores.txt in READ mode so the program can read data from it with READFILE. The file must already exist, and READ mode does not change its contents.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR READ
Q2
OPENFILE "Scores.txt" FOR READ
Q3
OPENFILE "Scores.txt" FOR READ
Q4
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q5
DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Section E: Spot the Error
Q1
A — The program wants to read existing data, so the file must be opened in READ mode. FOR WRITE would replace the contents. It should be OPENFILE "Scores.txt" FOR READ.
Q2
B — The keyword FOR is missing before the file mode. It should be OPENFILE "Scores.txt" FOR READ.
Q3
D — READFILE needs a comma between the file identifier and the variable. It should be READFILE "Marks.txt", Mark.
Q4
C — The file was opened FOR READ, so the program must use READFILE to get data from it. WRITEFILE is for a file opened in WRITE mode. It should be READFILE "Scores.txt", Score.
Q5
B — The file identifier is a string, so it needs quotation marks. It should be OPENFILE "Scores.txt" FOR READ.