File modes: READ and WRITE

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

File modes: READ and WRITE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program must load saved high scores from a file. Which mode should it use?
A READ
B WRITE
2. A program must save a player's name into a file. Which mode should it use?
A READ
B WRITE
3. Which line opens a file so data can be put into it?
A OPENFILE "Scores.txt" FOR WRITE
B OPENFILE "Scores.txt" FOR READ
4. Which statement about file modes is true?
A A file mode states what the file is called
B A file mode states what the program will do with the open file
5. Which statement about modes is true?
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

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 READ AOpens the file in READ mode so the program can get data out of it
2OPENFILE "scores.txt" FOR WRITE BOPENFILE "best.txt" FOR WRITE
3The program needs to get saved data out of a file, so it chooses READ mode COpens the file in WRITE mode so the program can put data into it
4The program needs to save a new high score into a file, so it chooses WRITE mode DThe file is open in READ mode, but the program tries to write data to it. A file opened FOR READ can only be read.
5OPENFILE "data.txt" FOR READ WRITEFILE "data.txt", Score EThe file is open in WRITE mode, but the program tries to read data from it. A file opened FOR WRITE can only be written to.
6OPENFILE "data.txt" FOR WRITE READFILE "data.txt", Score FOPENFILE "names.txt" FOR READ

Definitions

file mode

Fill in the Blanks

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

1.
OPENFILE "marks.txt" FOR _____ WRITEFILE "marks.txt", Score CLOSEFILE "marks.txt"
2.
OPENFILE "names.txt" FOR ____ READFILE "names.txt", Name CLOSEFILE "names.txt"
3.
OPENFILE "log.txt" ___ READ READFILE "log.txt", Entry CLOSEFILE "log.txt"
4.
________ "data.txt" FOR READ READFILE "data.txt", Item CLOSEFILE "data.txt"
5.
// Load the saved score OPENFILE "best.txt" FOR ____ READFILE "best.txt", HighScore

Spot the Error

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

Question 1
A DECLARE Name : STRING
B OPENFILE "names.txt" FOR READ
C WRITEFILE "names.txt", Name
D CLOSEFILE "names.txt"
Question 2
A DECLARE Score : INTEGER
B OPENFILE "data.txt" FOR READ
C WRITEFILE "data.txt", Score
D CLOSEFILE "data.txt"
Question 3
A OPENFILE "pets.txt" FOR WRITE
B WRITEFILE "pets.txt", "Cat"
C READFILE "pets.txt", Pet
D CLOSEFILE "pets.txt"
Question 4
A OPENFILE "a.txt" FOR READ
B READFILE "a.txt", Line
C OPENFILE "a.txt" FOR WRITE
D OPENFILE "a.txt" FOR WRITE
E WRITEFILE "a.txt", Line
Question 5
A DECLARE Total : INTEGER
B OPENFILE "total.txt" READ
C READFILE "total.txt", Total
D OUTPUT Total

Answer Key

File modes: READ and WRITE
Section A: Quick Questions
Q1
A — READ
Q2
B — WRITE
Q3
A — OPENFILE "Scores.txt" FOR WRITE
Q4
B — A file mode states what the program will do with the open file
Q5
A — A file is open in only one mode at a time
Section B: Match the Pairs
Q1
OPENFILE "scores.txt" FOR READ → Opens the file in READ mode so the program can get data out of it
Q2
OPENFILE "scores.txt" FOR WRITE → Opens the file in WRITE mode so the program can put data into it
Q3
The program needs to get saved data out of a file, so it chooses READ mode → OPENFILE "names.txt" FOR READ
Q4
The program needs to save a new high score into a file, so it chooses WRITE mode → OPENFILE "best.txt" FOR WRITE
Q5
OPENFILE "data.txt" FOR READ WRITEFILE "data.txt", Score → The file is open in READ mode, but the program tries to write data to it. A file opened FOR READ can only be read.
Q6
OPENFILE "data.txt" FOR WRITE READFILE "data.txt", Score → The file is open in WRITE mode, but the program tries to read data from it. A file opened FOR WRITE can only be written to.
Section C: Definitions
Q1
file mode — States what a program will do with an open file: READ gets data out of it, WRITE puts data into it. A file is open in only one mode at a time, chosen by what the program needs.
Section D: Fill in the Blanks
Q1
OPENFILE "marks.txt" FOR WRITE WRITEFILE "marks.txt", Score CLOSEFILE "marks.txt"
Q2
OPENFILE "names.txt" FOR READ READFILE "names.txt", Name CLOSEFILE "names.txt"
Q3
OPENFILE "log.txt" FOR READ READFILE "log.txt", Entry CLOSEFILE "log.txt"
Q4
OPENFILE "data.txt" FOR READ READFILE "data.txt", Item CLOSEFILE "data.txt"
Q5
// Load the saved score OPENFILE "best.txt" FOR READ READFILE "best.txt", HighScore
Section E: Spot the Error
Q1
C — The file was opened FOR READ, so the program can only read from it. It must use READFILE "names.txt", Name to get data out. WRITEFILE needs the file to be open FOR WRITE.
Q2
B — The program wants to put Score into the file with WRITEFILE, so the file must be opened FOR WRITE. FOR READ only lets the program get data out of the file.
Q3
C — The file is open in WRITE mode, so the program cannot read from it. Line 3 should be WRITEFILE "pets.txt", "Dog" to put the next item into the file.
Q4
C — A file is open in only one mode at a time. The READ file must be closed with CLOSEFILE "a.txt" before it is opened again FOR WRITE on line 4.
Q5
B — The file mode must be introduced with the keyword FOR. The correct line is OPENFILE "total.txt" FOR READ.