Common mistakes in file handling pseudocode

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

Common mistakes in file handling pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which program reads a line from a file correctly?
A OPENFILE "notes.txt" FOR READ READFILE "notes.txt", Line
B OPENFILE "notes.txt" FOR WRITE READFILE "notes.txt", Line
2. Which program uses the file at the right time?
A OPENFILE "scores.txt" FOR READ READFILE "scores.txt", Score
B READFILE "scores.txt", Score OPENFILE "scores.txt" FOR READ
3. Which program finishes its file handling properly?
A OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", "Done"
B OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", "Done" CLOSEFILE "log.txt"
4. Which program uses the same file identifier throughout?
A OPENFILE "names.txt" FOR READ READFILE "names.txt", Name
B OPENFILE "names.txt" FOR READ READFILE "name.txt", Name
5. A file already holds 50 high scores that must be kept. Which line is safe if you only want to read them?
A OPENFILE "scores.txt" FOR WRITE
B OPENFILE "scores.txt" FOR READ

Match the Pairs

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

Left columnRight column
1OPENFILE "scores.txt" FOR WRITE READFILE "scores.txt", Score AThe file identifier in READFILE (mark.txt) is different from the one in OPENFILE (marks.txt), so the file names do not match
2OPENFILE "names.txt" FOR WRITE WRITEFILE "names.txt", Name BError: READFILE is used on a file opened for WRITE, so the program cannot read from it
3Every file must be opened with OPENFILE before READFILE or WRITEFILE can use it CCLOSEFILE is missing, so the file is never closed and the data may not be saved properly
4A file should be closed with CLOSEFILE once the program has finished using it DOPENFILE "data.txt" FOR READ READFILE "data.txt", Item
5OPENFILE "marks.txt" FOR READ READFILE "mark.txt", Mark EWRITEFILE "log.txt", Line CLOSEFILE "log.txt"
6OPENFILE "diary.txt" FOR WRITE WRITEFILE "diary.txt", Entry // program ends FError: the file is opened FOR WRITE, which wipes any old data in names.txt before the new data is saved

Definitions

file handling error

Fill in the Blanks

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

1.
OPENFILE "names.txt" FOR ____ READFILE "names.txt", Name CLOSEFILE "names.txt"
2.
________ "scores.txt" FOR READ READFILE "scores.txt", Score CLOSEFILE "scores.txt"
3.
OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", Message _________ "log.txt"
4.
OPENFILE "marks.txt" FOR READ READFILE "_________", Mark CLOSEFILE "marks.txt"
5.
// Old data is lost after this line OPENFILE "best.txt" FOR _____ WRITEFILE "best.txt", NewScore

Spot the Error

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

Question 1
A OPENFILE "data.txt" FOR WRITE
B READFILE "data.txt", Line
C OUTPUT Line
D CLOSEFILE "data.txt"
Question 2
A OPENFILE "log.txt" FOR WRITE
B WRITEFILE "log.txt", "Done"
C CLOSEFILE "log.text"
Question 3
A DECLARE Name : STRING
B OPENFILE "name.txt" FOR READ
C READFILE "names.txt", Name
D CLOSEFILE "names.txt"
Question 4
A OPENFILE "marks.txt" FOR READ
B WRITEFILE "marks.txt", Mark
C CLOSEFILE "marks.txt"
Question 5
A DECLARE Pet : STRING
B // open the file here
C READFILE "pets.txt", Pet
D CLOSEFILE "pets.txt"

Answer Key

Common mistakes in file handling pseudocode
Section A: Quick Questions
Q1
A — OPENFILE "notes.txt" FOR READ READFILE "notes.txt", Line
Q2
A — OPENFILE "scores.txt" FOR READ READFILE "scores.txt", Score
Q3
B — OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", "Done" CLOSEFILE "log.txt"
Q4
A — OPENFILE "names.txt" FOR READ READFILE "names.txt", Name
Q5
B — OPENFILE "scores.txt" FOR READ
Section B: Match the Pairs
Q1
OPENFILE "scores.txt" FOR WRITE READFILE "scores.txt", Score → Error: READFILE is used on a file opened for WRITE, so the program cannot read from it
Q2
OPENFILE "names.txt" FOR WRITE WRITEFILE "names.txt", Name → Error: the file is opened FOR WRITE, which wipes any old data in names.txt before the new data is saved
Q3
Every file must be opened with OPENFILE before READFILE or WRITEFILE can use it → OPENFILE "data.txt" FOR READ READFILE "data.txt", Item
Q4
A file should be closed with CLOSEFILE once the program has finished using it → WRITEFILE "log.txt", Line CLOSEFILE "log.txt"
Q5
OPENFILE "marks.txt" FOR READ READFILE "mark.txt", Mark → The file identifier in READFILE (mark.txt) is different from the one in OPENFILE (marks.txt), so the file names do not match
Q6
OPENFILE "diary.txt" FOR WRITE WRITEFILE "diary.txt", Entry // program ends → CLOSEFILE is missing, so the file is never closed and the data may not be saved properly
Section C: Definitions
Q1
file handling error — A mistake such as using the wrong mode, missing OPENFILE or CLOSEFILE, using different file names, or opening a file FOR WRITE and wiping its old data.
Section D: Fill in the Blanks
Q1
OPENFILE "names.txt" FOR READ READFILE "names.txt", Name CLOSEFILE "names.txt"
Q2
OPENFILE "scores.txt" FOR READ READFILE "scores.txt", Score CLOSEFILE "scores.txt"
Q3
OPENFILE "log.txt" FOR WRITE WRITEFILE "log.txt", Message CLOSEFILE "log.txt"
Q4
OPENFILE "marks.txt" FOR READ READFILE "marks.txt", Mark CLOSEFILE "marks.txt"
Q5
// Old data is lost after this line OPENFILE "best.txt" FOR WRITE WRITEFILE "best.txt", NewScore
Section E: Spot the Error
Q1
A — The program reads from the file, so it must be opened FOR READ. Opening it FOR WRITE would wipe the old data, and READFILE cannot be used on a file opened for WRITE.
Q2
C — The file identifier in CLOSEFILE must match the one used in OPENFILE. "log.text" is a different file, so log.txt is never closed. It should be CLOSEFILE "log.txt".
Q3
B — The file name in OPENFILE (name.txt) does not match the one in READFILE and CLOSEFILE (names.txt). All file identifiers must be identical: OPENFILE "names.txt" FOR READ.
Q4
A — WRITEFILE needs the file to be opened FOR WRITE. Opening it FOR READ is the wrong mode, so the data cannot be written. It should be OPENFILE "marks.txt" FOR WRITE.
Q5
B — The OPENFILE line is missing, so READFILE tries to use a file that has not been opened. The line should be OPENFILE "pets.txt" FOR READ.