READFILE: reading a line of text

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

READFILE: reading a line of text

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which statement reads one line of text from the open file Names.txt into a variable?
A READFILE "Names.txt", LineOfText
B READFILE LineOfText, "Names.txt"
2. Which declaration should come before READFILE "Names.txt", LineOfText to store a line of text?
A DECLARE LineOfText : STRING
B DECLARE LineOfText : INTEGER
3. Names.txt is open for READ. READFILE "Names.txt", LineOfText runs twice in a row. What happens?
A Both calls read line 1 of the file
B The first call reads line 1 and the second call reads line 2
4. Names.txt contains the lines Ava, Liam, Mia. After one READFILE "Names.txt", LineOfText, what does LineOfText hold?
A Ava
B Ava, Liam, Mia
5. Which line shows the line of text that was just read on the screen?
A READFILE LineOfText
B OUTPUT LineOfText

Match the Pairs

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

Left columnRight column
1READFILE "Names.txt", LineOfText ADECLARE LineOfText : STRING
2OUTPUT LineOfText BLineOfText is declared as INTEGER but a line of text must be stored in a STRING variable. It should be DECLARE LineOfText : STRING
3Declare the STRING variable first so it can hold a line of text read from the file CDisplays the line of text that READFILE has just stored in the variable
4Each READFILE call reads the next line, so two calls in a row read the first and then the second line DReads the next line of text from the open file Names.txt and stores it in the variable LineOfText
5READFILE "Names.txt", "LineOfText" EREADFILE "Names.txt", LineOfText READFILE "Names.txt", LineOfText
6DECLARE LineOfText : INTEGER READFILE "Names.txt", LineOfText FThe variable name is in quotes, so it is a string literal and not a variable. It should be written without quotes: READFILE "Names.txt", LineOfText

Definitions

READFILE "Names.txt", LineOfText

Fill in the Blanks

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

1.
________ "Names.txt", LineOfText
2.
DECLARE LineOfText : ______ READFILE "Names.txt", LineOfText
3.
READFILE "Names.txt", __________ OUTPUT LineOfText
4.
READFILE "Names.txt", LineOfText ______ LineOfText
5.
OPENFILE "Names.txt" FOR ____ READFILE "Names.txt", LineOfText OUTPUT LineOfText

Spot the Error

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

Question 1
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt", TextLine
D OUTPUT "TextLine"
E CLOSEFILE "Names.txt"
Question 2
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR WRITE
C READFILE "Names.txt", TextLine
D OUTPUT TextLine
Question 3
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" READ
C READFILE "Names.txt", TextLine
D OUTPUT TextLine
E CLOSEFILE "Names.txt"
Question 4
A DECLARE TextLine : STRING
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt" TextLine
D OUTPUT TextLine
E READFILE "Names.txt", TextLine
F OUTPUT TextLine
Question 5
A DECLARE TextLine : INTEGER
B OPENFILE "Names.txt" FOR READ
C READFILE "Names.txt", TextLine
D OUTPUT TextLine

Answer Key

READFILE: reading a line of text
Section A: Quick Questions
Q1
A — READFILE "Names.txt", LineOfText
Q2
A — DECLARE LineOfText : STRING
Q3
B — The first call reads line 1 and the second call reads line 2
Q4
A — Ava
Q5
B — OUTPUT LineOfText
Section B: Match the Pairs
Q1
READFILE "Names.txt", LineOfText → Reads the next line of text from the open file Names.txt and stores it in the variable LineOfText
Q2
OUTPUT LineOfText → Displays the line of text that READFILE has just stored in the variable
Q3
Declare the STRING variable first so it can hold a line of text read from the file → DECLARE LineOfText : STRING
Q4
Each READFILE call reads the next line, so two calls in a row read the first and then the second line → READFILE "Names.txt", LineOfText READFILE "Names.txt", LineOfText
Q5
READFILE "Names.txt", "LineOfText" → The variable name is in quotes, so it is a string literal and not a variable. It should be written without quotes: READFILE "Names.txt", LineOfText
Q6
DECLARE LineOfText : INTEGER READFILE "Names.txt", LineOfText → LineOfText is declared as INTEGER but a line of text must be stored in a STRING variable. It should be DECLARE LineOfText : STRING
Section C: Definitions
Q1
READFILE "Names.txt", LineOfText — Reads the next line of text from the open file Names.txt into the STRING variable LineOfText. Each READFILE call reads the following line. Declare the variable first.
Section D: Fill in the Blanks
Q1
READFILE "Names.txt", LineOfText
Q2
DECLARE LineOfText : STRING READFILE "Names.txt", LineOfText
Q3
READFILE "Names.txt", LineOfText OUTPUT LineOfText
Q4
READFILE "Names.txt", LineOfText OUTPUT LineOfText
Q5
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", LineOfText OUTPUT LineOfText
Section E: Spot the Error
Q1
D — Quotes make OUTPUT display the words TextLine instead of the line read from the file. It should be OUTPUT TextLine, with no quotes, so the variable's contents are displayed.
Q2
B — READFILE needs the file opened in READ mode. WRITE mode would replace the file's data with an empty file. It should be OPENFILE "Names.txt" FOR READ.
Q3
B — The keyword FOR is missing. The correct syntax is OPENFILE "Names.txt" FOR READ.
Q4
C — A comma is missing between the file name and the variable. It should be READFILE "Names.txt", TextLine.
Q5
A — A line of text must be stored in a STRING variable. An INTEGER variable cannot hold text. It should be DECLARE TextLine : STRING.