OPENFILE ... FOR WRITE

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

OPENFILE ... FOR WRITE

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly opens the file Scores.txt so data can be written to it?
A OPENFILE Scores.txt FOR WRITE
B OPENFILE "Scores.txt" FOR WRITE
2. What does FOR WRITE tell the program in OPENFILE "Scores.txt" FOR WRITE?
A The file is being opened so data can be written into it
B The file is being opened so data can be read from it
3. Which order of statements is correct?
A WRITEFILE, then OPENFILE "Scores.txt" FOR WRITE
B OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE
4. Which is the correct way to write the keywords in 0478 pseudocode?
A OPENFILE "Scores.txt" FOR WRITE
B openfile "Scores.txt" for write
5. True or false: it is good practice to state the mode (FOR WRITE) every time you open a file.
A True: the mode makes clear what the program will do with the file
B False: the mode can be left out when opening a file

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 AOPENFILE "Marks.txt" FOR WRITE
2OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name BThe file is opened first, then data is written into it
3The file name is a string in quotes, then the mode FOR WRITE states that you will write to the file CThe file name is missing its quotation marks. The file identifier must be a string such as "Scores.txt".
4Keywords such as OPENFILE and FOR WRITE are written in capital letters in 0478 pseudocode DWRITEFILE is used before the file is opened. OPENFILE ... FOR WRITE must come first.
5WRITEFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR WRITE EOpens the file Scores.txt in write mode so data can be written into it
6OPENFILE Scores.txt FOR WRITE FOPENFILE "Results.txt" FOR WRITE

Definitions

OPENFILE "Scores.txt" FOR WRITE

Fill in the Blanks

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

1.
________ "Scores.txt" FOR WRITE
2.
OPENFILE "Scores.txt" ___ WRITE
3.
OPENFILE "Scores.txt" FOR _____
4.
OPENFILE "Scores.txt" FOR WRITE _________ "Scores.txt", Score
5.
OPENFILE "Names.txt" FOR _____ WRITEFILE "Names.txt", Name

Spot the Error

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

Question 1
A DECLARE Name : STRING
B Name ← "Ava"
C OPENFILE "Names.txt" FOR WRITE
D READFILE "Names.txt", Name
E CLOSEFILE "Names.txt"
Question 2
A DECLARE Score : INTEGER
B OPENFILE "Data.txt" FOR READ
C Score ← 85
D WRITEFILE "Data.txt", Score
E CLOSEFILE "Data.txt"
Question 3
A DECLARE Mark : INTEGER
B Mark ← 72
C OPENFILE "Marks.txt" FOR WRITE
D WRITEFILE Mark, "Marks.txt"
E CLOSEFILE "Marks.txt"
Question 4
A DECLARE Item : STRING
B Item ← "Pen"
C OPEN "Shop.txt" FOR WRITE
D WRITEFILE "Shop.txt", Item
E CLOSEFILE "Shop.txt"
Question 5
A DECLARE Age : INTEGER
B OPENFILE "Ages.txt" WRITE
C Age ← 16
D WRITEFILE "Ages.txt", Age
E CLOSEFILE "Ages.txt"

Answer Key

OPENFILE ... FOR WRITE
Section A: Quick Questions
Q1
B — OPENFILE "Scores.txt" FOR WRITE
Q2
A — The file is being opened so data can be written into it
Q3
B — OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE
Q4
A — OPENFILE "Scores.txt" FOR WRITE
Q5
A — True: the mode makes clear what the program will do with the file
Section B: Match the Pairs
Q1
OPENFILE "Scores.txt" FOR WRITE → Opens the file Scores.txt in write mode so data can be written into it
Q2
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name → The file is opened first, then data is written into it
Q3
The file name is a string in quotes, then the mode FOR WRITE states that you will write to the file → OPENFILE "Results.txt" FOR WRITE
Q4
Keywords such as OPENFILE and FOR WRITE are written in capital letters in 0478 pseudocode → OPENFILE "Marks.txt" FOR WRITE
Q5
WRITEFILE "Scores.txt", Score OPENFILE "Scores.txt" FOR WRITE → WRITEFILE is used before the file is opened. OPENFILE ... FOR WRITE must come first.
Q6
OPENFILE Scores.txt FOR WRITE → The file name is missing its quotation marks. The file identifier must be a string such as "Scores.txt".
Section C: Definitions
Q1
OPENFILE "Scores.txt" FOR WRITE — Opens the file Scores.txt in write mode so data can be written into it with WRITEFILE. The file must be opened before writing. Keywords are in capitals.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR WRITE
Q2
OPENFILE "Scores.txt" FOR WRITE
Q3
OPENFILE "Scores.txt" FOR WRITE
Q4
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Q5
OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name
Section E: Spot the Error
Q1
D — The file was opened FOR WRITE, so data must be put into it with WRITEFILE. READFILE reads data out of a file. The line should be WRITEFILE "Names.txt", Name.
Q2
B — The program wants to write Score into the file, so the file must be opened in write mode. FOR READ only allows reading. It should be OPENFILE "Data.txt" FOR WRITE.
Q3
D — WRITEFILE needs the file identifier first, then a comma, then the data to write. The arguments are the wrong way round. It should be WRITEFILE "Marks.txt", Mark.
Q4
C — The 0478 pseudocode keyword is OPENFILE, not OPEN. The line should be OPENFILE "Shop.txt" FOR WRITE.
Q5
B — The keyword FOR is missing before the mode. The correct form is OPENFILE <File identifier> FOR WRITE, so the line should be OPENFILE "Ages.txt" FOR WRITE.