Why a file must be closed

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

Why a file must be closed

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why do we use CLOSEFILE when we have finished with a file?
A So the data is safely saved and the file can be opened again
B So the file is deleted from the computer
2. A program writes names to a file, then wants to read them back. What must happen in between?
A OPENFILE "Names.txt" FOR READ straight away, with no CLOSEFILE
B CLOSEFILE "Names.txt" then OPENFILE "Names.txt" FOR READ
3. Which statement about file modes is correct?
A A file is open in only one mode at a time
B A file can be open in READ and WRITE mode at the same time
4. Which program part is correct?
A OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score (no CLOSEFILE at the end)
B OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
5. A program opens two files, InFile.txt and OutFile.txt. How many CLOSEFILE statements are needed?
A Two: one CLOSEFILE for each OPENFILE
B One: a single CLOSEFILE closes every file

Match the Pairs

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

Left columnRight column
1CLOSEFILE("Scores.txt") ACLOSEFILE is missing after writing. The file is still open in WRITE mode, so it cannot be opened for READ and the data may not be saved. Add CLOSEFILE("Scores.txt") before the second OPENFILE.
2OPENFILE "Scores.txt" FOR WRITE CLOSEFILE("Scores.txt") OPENFILE "Scores.txt" FOR READ BThe file is closed after WRITE so it can then be opened in READ mode
3Every OPENFILE needs its own matching CLOSEFILE, so two open files need two CLOSEFILE statements CWRITEFILE("Names.txt", Name) CLOSEFILE("Names.txt") OPENFILE "Names.txt" FOR READ
4A file is open in only one mode at a time, so close it after writing before reading the data back DOnly one CLOSEFILE is used for two OPENFILE statements. A.txt is never closed. Add CLOSEFILE("A.txt") so there is one CLOSEFILE for each OPENFILE.
5OPENFILE "Scores.txt" FOR WRITE WRITEFILE("Scores.txt", Score) OPENFILE "Scores.txt" FOR READ ECloses the open file so its data is safely saved and it is free to be opened again in a different mode
6OPENFILE "A.txt" FOR READ OPENFILE "B.txt" FOR WRITE CLOSEFILE("B.txt") FOPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE("In.txt") CLOSEFILE("Out.txt")

Definitions

CLOSEFILE

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.
CLOSEFILE "Scores.txt" OPENFILE "Scores.txt" FOR ____ READFILE "Scores.txt", Score
3.
________ "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
4.
OPENFILE "Log.txt" FOR _____ WRITEFILE "Log.txt", Entry CLOSEFILE "Log.txt"
5.
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score _________ "Scores.txt"

Spot the Error

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

Question 1
A OPENFILE "mark.txt" FOR READ
B WRITEFILE "mark.txt", Score
C CLOSEFILE "mark.txt"
Question 2
A OPENFILE "log.txt" FOR WRITE
B WRITEFILE "log.txt", Msg
C CLOSEFILE "notes.txt"
D OPENFILE "log.txt" FOR READ
Question 3
A DECLARE Name : STRING
B OPENFILE "names.txt" FOR READ
C WRITEFILE "names.txt", Name
D CLOSEFILE "names.txt"
Question 4
A OPENFILE "a.txt" FOR WRITE
B WRITEFILE "a.txt", Data
C CLOSEFILE "a.txt"
D OPENFILE "a.txt" FOR WRITE
E READFILE "a.txt", Data
F CLOSEFILE "a.txt"
Question 5
A OPEN "data.txt" FOR WRITE
B WRITEFILE "data.txt", Total
C CLOSEFILE "data.txt"

Answer Key

Why a file must be closed
Section A: Quick Questions
Q1
A — So the data is safely saved and the file can be opened again
Q2
B — CLOSEFILE "Names.txt" then OPENFILE "Names.txt" FOR READ
Q3
A — A file is open in only one mode at a time
Q4
B — OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Q5
A — Two: one CLOSEFILE for each OPENFILE
Section B: Match the Pairs
Q1
CLOSEFILE("Scores.txt") → Closes the open file so its data is safely saved and it is free to be opened again in a different mode
Q2
OPENFILE "Scores.txt" FOR WRITE CLOSEFILE("Scores.txt") OPENFILE "Scores.txt" FOR READ → The file is closed after WRITE so it can then be opened in READ mode
Q3
Every OPENFILE needs its own matching CLOSEFILE, so two open files need two CLOSEFILE statements → OPENFILE "In.txt" FOR READ OPENFILE "Out.txt" FOR WRITE CLOSEFILE("In.txt") CLOSEFILE("Out.txt")
Q4
A file is open in only one mode at a time, so close it after writing before reading the data back → WRITEFILE("Names.txt", Name) CLOSEFILE("Names.txt") OPENFILE "Names.txt" FOR READ
Q5
OPENFILE "Scores.txt" FOR WRITE WRITEFILE("Scores.txt", Score) OPENFILE "Scores.txt" FOR READ → CLOSEFILE is missing after writing. The file is still open in WRITE mode, so it cannot be opened for READ and the data may not be saved. Add CLOSEFILE("Scores.txt") before the second OPENFILE.
Q6
OPENFILE "A.txt" FOR READ OPENFILE "B.txt" FOR WRITE CLOSEFILE("B.txt") → Only one CLOSEFILE is used for two OPENFILE statements. A.txt is never closed. Add CLOSEFILE("A.txt") so there is one CLOSEFILE for each OPENFILE.
Section C: Definitions
Q1
CLOSEFILE — Closes an open file so its data is safely saved and the file can be opened again in a different mode, such as READ after WRITE. Use one CLOSEFILE for each OPENFILE.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Q2
CLOSEFILE "Scores.txt" OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score
Q3
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt"
Q4
OPENFILE "Log.txt" FOR WRITE WRITEFILE "Log.txt", Entry CLOSEFILE "Log.txt"
Q5
OPENFILE "Scores.txt" FOR READ READFILE "Scores.txt", Score CLOSEFILE "Scores.txt"
Section E: Spot the Error
Q1
A — The program writes to the file on line 2, so it must be opened in WRITE mode. A file open for READ cannot be written to. Line 1 should be OPENFILE "mark.txt" FOR WRITE.
Q2
C — CLOSEFILE must use the same file name as the OPENFILE it matches. Here log.txt is never closed, so its data may not be saved and it cannot safely be opened in READ mode. It should be CLOSEFILE "log.txt".
Q3
C — The file was opened in READ mode, and a file is open in only one mode at a time. WRITEFILE cannot be used in READ mode. Line 3 should be READFILE "names.txt", Name.
Q4
D — After closing the file, the program wants to read the saved data back. Opening in WRITE mode would replace the data with an empty file. Line 4 should be OPENFILE "a.txt" FOR READ.
Q5
A — The keyword to open a file in 0478 pseudocode is OPENFILE, not OPEN. Line 1 should be OPENFILE "data.txt" FOR WRITE.