Tracing a file handling program

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

Tracing a file handling program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program runs OPENFILE 'a.txt' FOR WRITE, then WRITEFILE 'a.txt', 'Red', then WRITEFILE 'a.txt', 'Blue', then CLOSEFILE 'a.txt'. What does the trace table show in the file at the end?
A Red on line 1, Blue on line 2
B Only Blue, because the second WRITEFILE replaced Red
2. File data.txt holds the lines 5, 8, 2. The program runs OPENFILE FOR READ, then READFILE 'data.txt', X once. What is X in the trace table?
A 5
B 8
3. File scores.txt holds 3 lines. The program then runs OPENFILE 'scores.txt' FOR WRITE, WRITEFILE 'scores.txt', 'New', CLOSEFILE. What is in the file now?
A The 3 old lines followed by New
B Only New
4. A file holds the lines 'Cat' and 'Dog'. The program reads them with READFILE 'f.txt', A and then READFILE 'f.txt', B, then runs OUTPUT B. What is the final OUTPUT?
A Cat
B Dog
5. When should you update a trace table for a file program?
A After every file command, recording the file and variable values
B Only once at the end of the program

Match the Pairs

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

Left columnRight column
1WRITEFILE "Scores.txt", 45 AOUTPUT Total
2READFILE "Scores.txt", Num BAdds the value 45 as the next item in the file, so the trace table's file column gains a new entry.
3Reopening a file for WRITE erases the old data, so the trace table shows the file as empty before the new writes. CThe first READFILE reads the first unread item, so X should be 5, not 8.
4The final OUTPUT is found by reading the last value recorded in the variable column of the trace table. DCopies the next unread item from the file into Num, so the trace table shows Num's new value.
5File after WRITEFILE "A.txt", 5 then 8: only 8 EThe trace forgot that each WRITEFILE adds an item. After both commands the file holds 5 and 8, not just 8.
6READFILE "A.txt", X traced as X = 8 (file holds 5, 8) FOPENFILE "Log.txt" FOR WRITE

Definitions

trace table

Fill in the Blanks

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

1.
OPENFILE "N.txt" FOR _____ // N.txt is now empty: old data erased WRITEFILE "N.txt", "Zoe"
2.
_________ "N.txt", Name // File now holds the value of Name CLOSEFILE "N.txt"
3.
DECLARE Name : STRING READFILE "N.txt", ____ OUTPUT Name
4.
READFILE "N.txt", Name READFILE "N.txt", Name ______ Name
5.
CLOSEFILE "N.txt" OPENFILE "N.txt" FOR ____ READFILE "N.txt", Name

Spot the Error

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

Question 1
A OPENFILE "a.txt" FOR WRITE
B WRITEFILE "a.txt", "Hi"
C CLOSEFILE "b.txt"
D OPENFILE "a.txt" FOR READ
E READFILE "a.txt", Word
F OUTPUT Word
Question 2
A DECLARE Line : STRING
B OPENFILE "n.txt" FOR READ
C READFILE "n.txt", Line
D OUTPUT Lines
E CLOSEFILE "n.txt"
Question 3
A OPENFILE "t.txt" FOR WRITE
B WRITEFILE "t.txt", "Red"
C WRITEFILE "Blue", "t.txt"
D CLOSEFILE "t.txt"
Question 4
A DECLARE Item : STRING
B OPENFILE "x.txt" READ
C READFILE "x.txt", Item
D OUTPUT Item
Question 5
A OPENFILE "s.txt" FOR READ
B WRITEFILE "s.txt", "Cat"
C CLOSEFILE "s.txt"

Answer Key

Tracing a file handling program
Section A: Quick Questions
Q1
A — Red on line 1, Blue on line 2
Q2
A — 5
Q3
B — Only New
Q4
B — Dog
Q5
A — After every file command, recording the file and variable values
Section B: Match the Pairs
Q1
WRITEFILE "Scores.txt", 45 → Adds the value 45 as the next item in the file, so the trace table's file column gains a new entry.
Q2
READFILE "Scores.txt", Num → Copies the next unread item from the file into Num, so the trace table shows Num's new value.
Q3
Reopening a file for WRITE erases the old data, so the trace table shows the file as empty before the new writes. → OPENFILE "Log.txt" FOR WRITE
Q4
The final OUTPUT is found by reading the last value recorded in the variable column of the trace table. → OUTPUT Total
Q5
File after WRITEFILE "A.txt", 5 then 8: only 8 → The trace forgot that each WRITEFILE adds an item. After both commands the file holds 5 and 8, not just 8.
Q6
READFILE "A.txt", X traced as X = 8 (file holds 5, 8) → The first READFILE reads the first unread item, so X should be 5, not 8.
Section C: Definitions
Q1
trace table — A table that follows a program step by step, recording the file contents and each variable's value after every file command, until you find the final OUTPUT.
Section D: Fill in the Blanks
Q1
OPENFILE "N.txt" FOR WRITE // N.txt is now empty: old data erased WRITEFILE "N.txt", "Zoe"
Q2
WRITEFILE "N.txt", Name // File now holds the value of Name CLOSEFILE "N.txt"
Q3
DECLARE Name : STRING READFILE "N.txt", Name OUTPUT Name
Q4
READFILE "N.txt", Name READFILE "N.txt", Name OUTPUT Name
Q5
CLOSEFILE "N.txt" OPENFILE "N.txt" FOR READ READFILE "N.txt", Name
Section E: Spot the Error
Q1
C — CLOSEFILE must use the same file name that was opened. "b.txt" was never opened, so a.txt stays open and the trace is wrong. It should be CLOSEFILE "a.txt".
Q2
D — The value read from the file is stored in the variable Line. Lines is not declared and holds nothing in the trace table. It should be OUTPUT Line.
Q3
C — WRITEFILE needs the file name first and then the data. The arguments are swapped. It should be WRITEFILE "t.txt", "Blue".
Q4
B — OPENFILE needs the keyword FOR before the mode. Without it the file is never opened, so Item cannot be traced. It should be OPENFILE "x.txt" FOR READ.
Q5
A — The program uses WRITEFILE on line 2, so the file must be opened in WRITE mode. A file opened FOR READ cannot be written to. It should be OPENFILE "s.txt" FOR WRITE.