Declaring a variable to hold file data

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

Declaring a variable to hold file data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly declares a variable to hold a line of text read from a file?
A DECLARE LineOfText STRING =
B DECLARE LineOfText : STRING
2. Which line correctly declares a variable to hold a whole number, such as a score, that will be written to a file?
A DECLARE Score : INTEGER
B DECLARE Score : STRING
3. When should DECLARE LineOfText : STRING appear in the program?
A Before the READFILE command that stores a line in LineOfText
B After the READFILE command that stores a line in LineOfText
4. Which identifier is the more meaningful choice for a variable that holds a line of text read from a file?
A LineOfText
B x
5. What does DECLARE do in a file handling program?
A It opens the file so that data can be read from it
B It creates a variable that can hold data read from or written to a file

Match the Pairs

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

Left columnRight column
1DECLARE LineOfText : STRING AThe colon is missing between the identifier and the data type. It should be DECLARE LineOfText : STRING.
2DECLARE Score : INTEGER BDECLARE PlayerName : STRING
3Declare the variable first, then use it to store a line read from the file CCreates a variable called LineOfText that can hold a line of text read from or written to a file
4Give the variable a meaningful name and the correct data type for the file data DCreates a variable called Score that can hold a whole number, such as a value read from a file
5READFILE "Notes.txt", LineOfText DECLARE LineOfText : STRING EThe variable is declared after READFILE uses it. DECLARE must come before the file command that uses the variable.
6DECLARE LineOfText STRING FDECLARE LineOfText : STRING READFILE "Notes.txt", LineOfText

Definitions

DECLARE LineOfText : STRING

Fill in the Blanks

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

1.
_______ LineOfText : STRING
2.
DECLARE LineOfText : ______
3.
DECLARE Score : _______
4.
DECLARE LineOfText : STRING OPENFILE "Notes.txt" FOR READ READFILE "Notes.txt", __________
5.
DECLARE _____ : INTEGER OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score

Spot the Error

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

Question 1
A DECLARE LineOfText STRING
B OPENFILE "notes.txt" FOR READ
C READFILE "notes.txt", LineOfText
D CLOSEFILE "notes.txt"
Question 2
A DECLARE Score : INTEGER
B DECLARE LineOfText = STRING
C OPENFILE "data.txt" FOR READ
D READFILE "data.txt", LineOfText
Question 3
A DECLARE LineOfText : STRING
B OPENFILE "log.txt" FOR READ
C READFILE "log.txt", LineText
D CLOSEFILE "log.txt"
Question 4
A DECLARE Score : INTEGER
B Score ← 50
C OPENFILE "score.txt" FOR WRTE
D WRITEFILE "score.txt", Score
E CLOSEFILE "score.txt"
Question 5
A DECLARE Name : STRING
B DECLARE Score : INTEGER
C Score ← "ten"
D Name ← "Ava"

Answer Key

Declaring a variable to hold file data
Section A: Quick Questions
Q1
B — DECLARE LineOfText : STRING
Q2
A — DECLARE Score : INTEGER
Q3
A — Before the READFILE command that stores a line in LineOfText
Q4
A — LineOfText
Q5
B — It creates a variable that can hold data read from or written to a file
Section B: Match the Pairs
Q1
DECLARE LineOfText : STRING → Creates a variable called LineOfText that can hold a line of text read from or written to a file
Q2
DECLARE Score : INTEGER → Creates a variable called Score that can hold a whole number, such as a value read from a file
Q3
Declare the variable first, then use it to store a line read from the file → DECLARE LineOfText : STRING READFILE "Notes.txt", LineOfText
Q4
Give the variable a meaningful name and the correct data type for the file data → DECLARE PlayerName : STRING
Q5
READFILE "Notes.txt", LineOfText DECLARE LineOfText : STRING → The variable is declared after READFILE uses it. DECLARE must come before the file command that uses the variable.
Q6
DECLARE LineOfText STRING → The colon is missing between the identifier and the data type. It should be DECLARE LineOfText : STRING.
Section C: Definitions
Q1
DECLARE LineOfText : STRING — Creates a variable named LineOfText that holds text. Declare it before the file commands that use it, for example before READFILE stores a line in it.
Section D: Fill in the Blanks
Q1
DECLARE LineOfText : STRING
Q2
DECLARE LineOfText : STRING
Q3
DECLARE Score : INTEGER
Q4
DECLARE LineOfText : STRING OPENFILE "Notes.txt" FOR READ READFILE "Notes.txt", LineOfText
Q5
DECLARE Score : INTEGER OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Section E: Spot the Error
Q1
A — The colon is missing. A declaration needs the form DECLARE <identifier> : <data type>, so it should be DECLARE LineOfText : STRING.
Q2
B — A declaration uses a colon between the identifier and the data type, not an equals sign. It should be DECLARE LineOfText : STRING.
Q3
C — LineText was never declared. The variable that READFILE stores the data in must be the declared one, so it should be READFILE "log.txt", LineOfText.
Q4
C — WRTE is a spelling mistake. The file mode must be spelled WRITE, so it should be OPENFILE "score.txt" FOR WRITE.
Q5
C — Score was declared as INTEGER, so it cannot hold text. It should be assigned a whole number, such as Score ← 10.