WRITE mode creates a new file and replaces old data

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

WRITE mode creates a new file and replaces old data

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program runs OPENFILE "Scores.txt" FOR WRITE and no file called Scores.txt exists yet. What happens?
A The program stops with an error because the file is missing
B A new, empty file called Scores.txt is created
2. Scores.txt already holds 50 lines of data. A program runs OPENFILE "Scores.txt" FOR WRITE. What happens to the old data?
A It is kept and new data is added after it
B It is erased and replaced
3. Which statement about FOR WRITE is correct?
A Opening an existing file FOR WRITE deletes what was in it
B Opening an existing file FOR WRITE keeps its data safe until CLOSEFILE
4. A program runs OPENFILE "Names.txt" FOR WRITE twice, writing different data each time. What does Names.txt contain at the end?
A Only the data from the second run
B The data from both runs together
5. Which approach protects data you still need?
A Check whether the file already exists, or use a different file name, before opening it FOR WRITE
B Open the file FOR WRITE first and hope the old data is still there

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 ACreates Scores.txt if it does not exist; if it does exist, all the old data in it is erased
2OPENFILE "Log.txt" FOR WRITE (run a second time) BWipes everything saved in Log.txt by the first run, so only new data will be there
3If no file called Names.txt exists, opening it for WRITE makes a new empty file with that name COpening Scores.txt for WRITE erases the 50 saved scores, leaving only NewScore. The old data should have been read or copied first.
4To keep the old data in Scores.txt, read it first (READ mode) before opening the file for WRITE DThe programmer wrongly thinks Day2Text will be added after the Day 1 data. Opening for WRITE deletes the Day 1 data first.
5// Scores.txt holds 50 saved scores OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", NewScore EOPENFILE "Names.txt" FOR WRITE
6// Day 1 data is saved in Diary.txt OPENFILE "Diary.txt" FOR WRITE WRITEFILE "Diary.txt", Day2Text FOPENFILE "Scores.txt" FOR READ

Definitions

OPENFILE "Scores.txt" FOR WRITE

Fill in the Blanks

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

1.
OPENFILE "Scores.txt" FOR _____
2.
________ "Scores.txt" FOR WRITE
3.
OPENFILE "Log.txt" FOR WRITE WRITEFILE "Log.txt", "Day 1" _________ "Log.txt"
4.
OPENFILE "New.txt" ___ WRITE
5.
Score ← 50 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "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", "Ava"
C WRITEFILE Data.txt, "Liam"
D CLOSEFILE "Data.txt"
Question 2
A OUTPUT "Saving best score"
B OPENFILE "Best.txt" FOR READ
C WRITEFILE "Best.txt", Score
D CLOSEFILE "Best.txt"
Question 3
A DECLARE Name : STRING
B OPENFILE "Names.txt" FOR WRITE
C WRITEFILE "Names.txt", Name
D CLOSE "Names.txt"
Question 4
A INPUT Name
B OPENFILE "Log.txt" TO WRITE
C WRITEFILE "Log.txt", Name
D CLOSEFILE "Log.txt"
Question 5
A OPENFILE "Log.txt" FOR WRITE
B WRITEFILE "Log.txt" "Day 1"
C CLOSEFILE "Log.txt"
D OPENFILE "Log.txt" FOR WRITE
E WRITEFILE "Log.txt", "Day 2"

Answer Key

WRITE mode creates a new file and replaces old data
Section A: Quick Questions
Q1
B — A new, empty file called Scores.txt is created
Q2
B — It is erased and replaced
Q3
A — Opening an existing file FOR WRITE deletes what was in it
Q4
A — Only the data from the second run
Q5
A — Check whether the file already exists, or use a different file name, before opening it FOR WRITE
Section B: Match the Pairs
Q1
OPENFILE "Scores.txt" FOR WRITE → Creates Scores.txt if it does not exist; if it does exist, all the old data in it is erased
Q2
OPENFILE "Log.txt" FOR WRITE (run a second time) → Wipes everything saved in Log.txt by the first run, so only new data will be there
Q3
If no file called Names.txt exists, opening it for WRITE makes a new empty file with that name → OPENFILE "Names.txt" FOR WRITE
Q4
To keep the old data in Scores.txt, read it first (READ mode) before opening the file for WRITE → OPENFILE "Scores.txt" FOR READ
Q5
// Scores.txt holds 50 saved scores OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", NewScore → Opening Scores.txt for WRITE erases the 50 saved scores, leaving only NewScore. The old data should have been read or copied first.
Q6
// Day 1 data is saved in Diary.txt OPENFILE "Diary.txt" FOR WRITE WRITEFILE "Diary.txt", Day2Text → The programmer wrongly thinks Day2Text will be added after the Day 1 data. Opening for WRITE deletes the Day 1 data first.
Section C: Definitions
Q1
OPENFILE "Scores.txt" FOR WRITE — Opens a file for writing. A new file is created if none exists. If it does exist, all its old data is erased and replaced, so check before overwriting data you need.
Section D: Fill in the Blanks
Q1
OPENFILE "Scores.txt" FOR WRITE
Q2
OPENFILE "Scores.txt" FOR WRITE
Q3
OPENFILE "Log.txt" FOR WRITE WRITEFILE "Log.txt", "Day 1" CLOSEFILE "Log.txt"
Q4
OPENFILE "New.txt" FOR WRITE
Q5
Score ← 50 OPENFILE "Scores.txt" FOR WRITE WRITEFILE "Scores.txt", Score
Section E: Spot the Error
Q1
C — The file name is a string, so it must be in quotes: WRITEFILE "Data.txt", "Liam". Without quotes it looks like a variable, not the file that was opened for WRITE.
Q2
B — READ mode only reads an existing file. It does not create a new file or replace old data. To save the score the file must be opened FOR WRITE.
Q3
D — The pseudocode keyword is CLOSEFILE, not CLOSE. It should be CLOSEFILE "Names.txt" so the new file is saved properly.
Q4
B — The file mode is introduced with the keyword FOR, not TO. It should be OPENFILE "Log.txt" FOR WRITE. If Log.txt is missing it is created, otherwise its old data is replaced.
Q5
B — A comma is needed between the file name and the data: WRITEFILE "Log.txt", "Day 1". Note that opening Log.txt FOR WRITE again on line 4 wipes Day 1, so only Day 2 remains.