READFILE: reading a single item of data

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

READFILE: reading a single item of data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line reads one item from Scores.txt into the variable Score?
A READFILE "Scores.txt", Score
B READFILE Score, "Scores.txt"
2. Which statement must run before READFILE "Scores.txt", Score?
A OPENFILE "Scores.txt" FOR WRITE
B OPENFILE "Scores.txt" FOR READ
3. After READFILE "Scores.txt", Score runs, where is the data item stored?
A In the variable Score
B In the file identifier "Scores.txt"
4. Scores.txt holds whole numbers such as 42. Which declaration suits Score?
A DECLARE Score : BOOLEAN
B DECLARE Score : INTEGER
5. What does one READFILE "Scores.txt", Score statement do?
A Reads one data item from the open file
B Reads every item in the file at once

Match the Pairs

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

Left columnRight column
1READFILE "Scores.txt", Score AReads one data item from the open file Scores.txt and stores it in the variable Score
2OPENFILE "Scores.txt" FOR READ BDECLARE Score : INTEGER READFILE "Scores.txt", Score
3The item read from the file is stored in the variable named after the file identifier CThere is no OPENFILE ... FOR READ before READFILE, so the file is not open and cannot be read.
4The variable's data type must match the item being read, so an integer score needs an INTEGER variable DMust be done before READFILE so the file is open in READ mode and ready to be read
5READFILE Score, "Scores.txt" EREADFILE "Names.txt", PlayerName
6READFILE "Scores.txt", Score CLOSEFILE "Scores.txt" FThe arguments are in the wrong order. The file identifier must come first, then the variable.

Definitions

READFILE "Scores.txt", Score

Fill in the Blanks

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

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

Spot the Error

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

Question 1
A DECLARE Score : INTEGER
B OPENFILE "Marks.txt" FOR WRITE
C READFILE "Marks.txt", Score
D OUTPUT Score
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Scores.txt" FOR READ
C READFILE "Scores.txt" Score
D CLOSEFILE "Scores.txt"
Question 3
A DECLARE Score : STRING
B OPENFILE "Scores.txt" FOR READ
C READFILE "Scores.txt", Score
D OUTPUT Score + 10
Question 4
A DECLARE Name : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE Name, "Names.txt"
D OUTPUT Name
Question 5
A DECLARE Mark : INTEGER
B OPENFILE "Marks.txt" READ
C READFILE "Marks.txt", Mark
D OUTPUT Mark

Answer Key

READFILE: reading a single item of data
Section A: Quick Questions
Q1
A — READFILE "Scores.txt", Score
Q2
B — OPENFILE "Scores.txt" FOR READ
Q3
A — In the variable Score
Q4
B — DECLARE Score : INTEGER
Q5
A — Reads one data item from the open file
Section B: Match the Pairs
Q1
READFILE "Scores.txt", Score → Reads one data item from the open file Scores.txt and stores it in the variable Score
Q2
OPENFILE "Scores.txt" FOR READ → Must be done before READFILE so the file is open in READ mode and ready to be read
Q3
The item read from the file is stored in the variable named after the file identifier → READFILE "Names.txt", PlayerName
Q4
The variable's data type must match the item being read, so an integer score needs an INTEGER variable → DECLARE Score : INTEGER READFILE "Scores.txt", Score
Q5
READFILE Score, "Scores.txt" → The arguments are in the wrong order. The file identifier must come first, then the variable.
Q6
READFILE "Scores.txt", Score CLOSEFILE "Scores.txt" → There is no OPENFILE ... FOR READ before READFILE, so the file is not open and cannot be read.
Section C: Definitions
Q1
READFILE "Scores.txt", Score — Reads one data item from the open file and stores it in the variable. The file must already be open in READ mode, and the variable's data type must match the item.
Section D: Fill in the Blanks
Q1
DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q2
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q3
DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q4
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q5
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score OUTPUT Score
Section E: Spot the Error
Q1
B — READFILE only works on a file that is open in READ mode. The file was opened FOR WRITE, so line 2 should end with FOR READ.
Q2
C — READFILE needs a comma between the file identifier and the variable: READFILE "Scores.txt", Score.
Q3
A — The file item is a whole number and the program adds 10 to it. The variable's data type must match the item, so Score should be declared as INTEGER, not STRING.
Q4
C — The arguments are the wrong way round. The file identifier comes first and the variable second: READFILE "Names.txt", Name.
Q5
B — The keyword FOR is missing. The correct line is OPENFILE "Marks.txt" FOR READ, so the file is open in READ mode before READFILE.