Reading several items from a file with a FOR loop

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

Reading several items from a file with a FOR loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which program reads 5 items from an open file and displays each one?
A FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item NEXT Count
B FOR Count ← 1 TO 5 OUTPUT Item NEXT Count
2. Where should OPENFILE ... FOR READ go when you read several items with a FOR loop?
A Inside the loop, so the file is opened on every repeat
B Before the FOR line, so the file is opened once
3. Where does CLOSEFILE belong?
A After NEXT Count, once all items are read
B Inside the loop, just before NEXT Count
4. A file was written with the items 10, 20, 30. A FOR loop reads three items and outputs each. What is displayed?
A 10, 20, 30 (the order they were written)
B 30, 20, 10 (the newest item first)
5. How many items does FOR Count ← 1 TO 5 with READFILE inside read from the file?
A 1 item, then it reads the whole file at once
B 5 items, one on each repeat

Match the Pairs

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

Left columnRight column
1FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count ACLOSEFILE is inside the loop, so the file is closed after the first repeat and the other items cannot be read. It should be after NEXT Count.
2CLOSEFILE "Scores.txt" BCloses the file once the loop has finished, so it goes after NEXT and not inside the loop
3Before the loop starts, the file must be opened for reading so READFILE has something to read from COPENFILE "Scores.txt" FOR READ
4Items come back in the order they were written, so each READFILE inside the loop gets the next item in the file DOPENFILE FOR READ is missing before the loop, so the file is never opened and READFILE cannot read from it. Add OPENFILE "Scores.txt" FOR READ first.
5FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score CLOSEFILE "Scores.txt" NEXT Count EReads 5 items from the open file, one on each repeat, and displays each one as it is read
6FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count CLOSEFILE "Scores.txt" FREADFILE "Scores.txt", Score

Definitions

FOR Count ← 1 TO 5

Fill in the Blanks

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

1.
OPENFILE "Scores.txt" FOR ____ FOR Count ← 1 TO 5 READFILE "Scores.txt", Item
2.
// The file holds 5 items FOR Count ← 1 TO _ READFILE "Scores.txt", Item
3.
FOR Count ← 1 TO 5 ________ "Scores.txt", Item OUTPUT Item
4.
READFILE "Scores.txt", Item ______ Item NEXT Count
5.
OUTPUT Item NEXT Count _________ "Scores.txt"

Spot the Error

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

Question 1
A OPENFILE "Marks.txt" FOR READ
B FOR Count ← 1 TO 5
C WRITEFILE "Marks.txt", Mark
D OUTPUT Mark
E NEXT Count
F CLOSEFILE "Marks.txt"
Question 2
A OPENFILE "Names.txt" FOR READ
B FOR Count ← 1 TO 4
C READFILE "Names.txt", Name
D OUTPUT "Name"
E NEXT Count
F CLOSEFILE "Names.txt"
Question 3
A OPENFILE "Temps.txt" FOR READ
B FOR Count ← 1 TO 7
C READFILE "Temps.txt", Temp
D OUTPUT Temps
E NEXT Count
F CLOSEFILE "Temps.txt"
Question 4
A OPENFILE "Prices.txt" FOR READ
B FOR Count ← 1 TILL 5
C READFILE "Prices.txt", Price
D OUTPUT Price
E NEXT Count
F CLOSEFILE "Prices.txt"
Question 5
A OPENFILE "Items.txt" FOR READ
B FOR Count ← 5 TO 1
C READFILE "Items.txt", Item
D OUTPUT Item
E NEXT Count
F CLOSEFILE "Items.txt"

Answer Key

Reading several items from a file with a FOR loop
Section A: Quick Questions
Q1
A — FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item NEXT Count
Q2
B — Before the FOR line, so the file is opened once
Q3
A — After NEXT Count, once all items are read
Q4
A — 10, 20, 30 (the order they were written)
Q5
B — 5 items, one on each repeat
Section B: Match the Pairs
Q1
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count → Reads 5 items from the open file, one on each repeat, and displays each one as it is read
Q2
CLOSEFILE "Scores.txt" → Closes the file once the loop has finished, so it goes after NEXT and not inside the loop
Q3
Before the loop starts, the file must be opened for reading so READFILE has something to read from → OPENFILE "Scores.txt" FOR READ
Q4
Items come back in the order they were written, so each READFILE inside the loop gets the next item in the file → READFILE "Scores.txt", Score
Q5
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score CLOSEFILE "Scores.txt" NEXT Count → CLOSEFILE is inside the loop, so the file is closed after the first repeat and the other items cannot be read. It should be after NEXT Count.
Q6
FOR Count ← 1 TO 5 READFILE "Scores.txt", Score OUTPUT Score NEXT Count CLOSEFILE "Scores.txt" → OPENFILE FOR READ is missing before the loop, so the file is never opened and READFILE cannot read from it. Add OPENFILE "Scores.txt" FOR READ first.
Section C: Definitions
Q1
FOR Count ← 1 TO 5 — Repeats READFILE and OUTPUT 5 times to read 5 items from an open file, one per repeat, in the order written. Put CLOSEFILE after NEXT.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR READ FOR Count ← 1 TO 5 READFILE "Scores.txt", Item
Q2
// The file holds 5 items FOR Count ← 1 TO 5 READFILE "Scores.txt", Item
Q3
FOR Count ← 1 TO 5 READFILE "Scores.txt", Item OUTPUT Item
Q4
READFILE "Scores.txt", Item OUTPUT Item NEXT Count
Q5
OUTPUT Item NEXT Count CLOSEFILE "Scores.txt"
Section E: Spot the Error
Q1
C — The file is open for reading, so the loop must use READFILE to bring each item into Mark. WRITEFILE would try to put data into the file. Line 3 should be READFILE "Marks.txt", Mark.
Q2
D — Quote marks make OUTPUT print the word Name instead of the data read from the file. The variable must not be in quotes: OUTPUT Name.
Q3
D — READFILE stores each item in the variable Temp, but OUTPUT uses a different name, Temps, which holds nothing. The variable names must match: OUTPUT Temp.
Q4
B — A count-controlled loop uses the keyword TO between the start and end values, not TILL. Line 2 should be FOR Count ← 1 TO 5.
Q5
B — The loop must count up from 1 to 5 so it repeats 5 times, once for each item in the file. Starting at 5 and going TO 1 does not repeat correctly. It should be FOR Count ← 1 TO 5.