CLOSEFILE

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

CLOSEFILE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly closes a file called Scores.txt in 0478 pseudocode?
A CLOSEFILE "Scores.txt"
B CLOSE "Scores.txt"
2. A program ran OPENFILE "Scores.txt" FOR WRITE. Which line closes it?
A CLOSEFILE "Scores.txt"
B CLOSEFILE "Marks.txt"
3. When should CLOSEFILE be used?
A After the last read or write on the file
B Before the file has been opened
4. A program opens two files, Input.txt and Output.txt. What should it do?
A Close only the first file it opened
B Close both files with CLOSEFILE
5. Which is the correct order of statements?
A OPENFILE, CLOSEFILE, WRITEFILE
B OPENFILE, WRITEFILE, CLOSEFILE

Match the Pairs

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

Left columnRight column
1CLOSEFILE "Scores.txt" ACloses the file named Scores.txt when the program has finished with it, so the data is saved and the file is released.
2OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" BCLOSEFILE is placed before the READFILE, so the file is closed before the data is read. It should come after the last read.
3The identifier in CLOSEFILE must match the identifier used in OPENFILE CThe file is closed straight after the last write, and the identifier is the same in all three statements.
4Every file that was opened must be closed, so two opened files need two CLOSEFILE statements DOPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE "In.txt" CLOSEFILE "Out.txt"
5OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Score.txt" EOPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
6OPENFILE "Scores.txt" FOR READ CLOSEFILE "Scores.txt" READFILE "Scores.txt", Score FThe identifier in CLOSEFILE ("Score.txt") does not match the one in OPENFILE ("Scores.txt"), so the opened file is never closed. It should be CLOSEFILE "Scores.txt".

Definitions

CLOSEFILE "Scores.txt"

Fill in the Blanks

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

1.
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score _________ "Scores.txt"
2.
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "__________"
3.
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name _________ "Names.txt"
4.
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "_________"
5.
READFILE "Scores.txt", Mark OUTPUT Mark _________ "Scores.txt"

Spot the Error

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

Question 1
A OPENFILE "Data.txt" FOR WRITE
B WRITEFILE "Data.txt", Score
C CLOSEFILE "Date.txt"
Question 2
A OPENFILE "Data.txt" FOR READ
B READFILE "Data.txt", Line
C OUTPUT Line
D CLOSEFILE Data.txt
Question 3
A OPENFILE "Mark.txt" FOR WRITE
B WRITEFILE "Marks.txt", Mark
C CLOSEFILE "Marks.txt"
Question 4
A OPENFILE "Data.txt" FOR READ
B READFILE "Data.txt", Line
C CLOSEFILE "Data.txt", Line
D OUTPUT Line
Question 5
A OPENFILE "Data.txt" FOR WRITE
B WRITEFILE "Data.txt", Name
C WRITEFILE "Data.txt", Score
D CLOSE "Data.txt"

Answer Key

CLOSEFILE
Section A: Quick Questions
Q1
A — CLOSEFILE "Scores.txt"
Q2
A — CLOSEFILE "Scores.txt"
Q3
A — After the last read or write on the file
Q4
B — Close both files with CLOSEFILE
Q5
B — OPENFILE, WRITEFILE, CLOSEFILE
Section B: Match the Pairs
Q1
CLOSEFILE "Scores.txt" → Closes the file named Scores.txt when the program has finished with it, so the data is saved and the file is released.
Q2
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt" → The file is closed straight after the last write, and the identifier is the same in all three statements.
Q3
The identifier in CLOSEFILE must match the identifier used in OPENFILE → OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Q4
Every file that was opened must be closed, so two opened files need two CLOSEFILE statements → OPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE "In.txt" CLOSEFILE "Out.txt"
Q5
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Score.txt" → The identifier in CLOSEFILE ("Score.txt") does not match the one in OPENFILE ("Scores.txt"), so the opened file is never closed. It should be CLOSEFILE "Scores.txt".
Q6
OPENFILE "Scores.txt" FOR READ CLOSEFILE "Scores.txt" READFILE "Scores.txt", Score → CLOSEFILE is placed before the READFILE, so the file is closed before the data is read. It should come after the last read.
Section C: Definitions
Q1
CLOSEFILE "Scores.txt" — Closes the file named in the identifier once you have finished reading or writing, so data is saved and the file is released. The name must match the one used in OPENFILE.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Q2
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Q3
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Q4
OPENFILE "Names.txt" FOR READ READFILE "Names.txt", Name CLOSEFILE "Names.txt"
Q5
READFILE "Scores.txt", Mark OUTPUT Mark CLOSEFILE "Scores.txt"
Section E: Spot the Error
Q1
C — The identifier in CLOSEFILE must match the one used in OPENFILE. "Date.txt" is a different file name, so it should be CLOSEFILE "Data.txt".
Q2
D — The file identifier is a string, so it needs quote marks. It should be CLOSEFILE "Data.txt".
Q3
A — The file opened is "Mark.txt", but the file written to and closed is "Marks.txt". The identifier must be the same in every statement: OPENFILE "Marks.txt" FOR WRITE.
Q4
C — CLOSEFILE takes only the file identifier. It does not take a variable. It should be CLOSEFILE "Data.txt".
Q5
D — The keyword is CLOSEFILE, not CLOSE. After the last write the program should use CLOSEFILE "Data.txt".