WRITEFILE: writing a single item of data

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

WRITEFILE: writing a single item of data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly writes the value of the variable Score into the file Scores.txt?
A WRITEFILE "Scores.txt", Score
B WRITEFILE Score, "Scores.txt"
2. True or false: WRITEFILE can write to a file that has been opened only in READ mode.
A True
B False
3. Score is an INTEGER variable holding 42. What does WRITEFILE "Scores.txt", Score put in the file?
A The value 42
B The word Score
4. Which order is needed for the two items after WRITEFILE?
A File identifier, then a comma, then the variable
B Variable, then a comma, then the file identifier
5. Which program order is correct?
A WRITEFILE "Scores.txt", Score, then OPENFILE "Scores.txt" FOR WRITE
B OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE "Scores.txt", Score

Match the Pairs

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

Left columnRight column
1WRITEFILE "Scores.txt", Score AWrites the value stored in the variable Score into the open file Scores.txt
2WRITEFILE "Names.txt", PlayerName BWrites the STRING held in PlayerName into the open file Names.txt
3Open the file in WRITE mode first, then use WRITEFILE to store the value of Score CWRITEFILE "Marks.txt", Mark
4The file identifier comes first, then a comma, then the variable to write DThe comma between the file identifier and the variable is missing.
5WRITEFILE Score, "Scores.txt" EThe arguments are the wrong way round. The file identifier must come first, then the comma, then the variable.
6WRITEFILE "Scores.txt" Score FOPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score

Definitions

WRITEFILE "Scores.txt", Score

Fill in the Blanks

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

1.
_________ "Scores.txt", Score
2.
Score ← 85 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", _____
3.
OPENFILE "Scores.txt" FOR _____ WRITEFILE "Scores.txt", Score
4.
Name ← "Ava" OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", ____
5.
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "__________", Score

Spot the Error

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

Question 1
A Score ← 85
B OPENFILE "Scores.txt" FOR READ
C WRITEFILE "Scores.txt", Score
D CLOSEFILE "Scores.txt"
Question 2
A Name ← "Ava"
B OPENFILE Names.txt FOR WRITE
C WRITEFILE "Names.txt", Name
D CLOSEFILE "Names.txt"
Question 3
A Score ← 72
B OPENFILE "Scores.txt" FOR WRITE
C Score ← Score + 1
D WRITEFILE "Scores.txt" Score
E CLOSEFILE "Scores.txt"
Question 4
A Age ← 16
B OPENFILE "Ages.txt" FOR WRITE
C WRITEFILE Age, "Ages.txt"
D CLOSEFILE "Ages.txt"
Question 5
A Name ← "Zoe"
B OPENFILE "Players.txt" FOR WRITE
C WRITEFILE "Player.txt", Name
D CLOSEFILE "Players.txt"

Answer Key

WRITEFILE: writing a single item of data
Section A: Quick Questions
Q1
A — WRITEFILE "Scores.txt", Score
Q2
B — False
Q3
A — The value 42
Q4
A — File identifier, then a comma, then the variable
Q5
B — OPENFILE "Scores.txt" FOR WRITE, then WRITEFILE "Scores.txt", Score
Section B: Match the Pairs
Q1
WRITEFILE "Scores.txt", Score → Writes the value stored in the variable Score into the open file Scores.txt
Q2
WRITEFILE "Names.txt", PlayerName → Writes the STRING held in PlayerName into the open file Names.txt
Q3
Open the file in WRITE mode first, then use WRITEFILE to store the value of Score → OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Q4
The file identifier comes first, then a comma, then the variable to write → WRITEFILE "Marks.txt", Mark
Q5
WRITEFILE Score, "Scores.txt" → The arguments are the wrong way round. The file identifier must come first, then the comma, then the variable.
Q6
WRITEFILE "Scores.txt" Score → The comma between the file identifier and the variable is missing.
Section C: Definitions
Q1
WRITEFILE "Scores.txt", Score — Writes the value stored in a variable into an open file. Give the file name, a comma, then the variable. The file must already be open in WRITE mode.
Section D: Fill in the Blanks
Q1
WRITEFILE "Scores.txt", Score
Q2
Score ← 85 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Q3
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Q4
Name ← "Ava" OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name
Q5
OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Section E: Spot the Error
Q1
B — WRITEFILE only works on a file opened in WRITE mode. Line 2 opens the file FOR READ, so it should be OPENFILE "Scores.txt" FOR WRITE.
Q2
B — The file identifier is a string, so the file name must be in quotes. It should be OPENFILE "Names.txt" FOR WRITE.
Q3
D — A comma is needed between the file identifier and the variable. It should be WRITEFILE "Scores.txt", Score.
Q4
C — The order is wrong. The file identifier comes first, then a comma, then the variable. It should be WRITEFILE "Ages.txt", Age.
Q5
C — The file identifier must match the name used in OPENFILE exactly. "Player.txt" is not the open file "Players.txt", so line 3 should use "Players.txt".