WRITEFILE: writing a line of text

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

WRITEFILE: writing a line of text

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line writes the text held in the STRING variable LineOfText to the file Names.txt?
A WRITEFILE "Names.txt", LineOfText
B WRITEFILE LineOfText, "Names.txt"
2. A program runs WRITEFILE "Names.txt", LineOfText three times in a row. How many lines does it add to the file?
A 1 line, because the file name is the same
B 3 lines, one for each WRITEFILE
3. Which variable declaration suits a variable that will hold a line of text to write to a file?
A DECLARE LineOfText : INTEGER
B DECLARE LineOfText : STRING
4. Which order is correct for writing a name typed in by the user?
A INPUT LineOfText, then WRITEFILE "Names.txt", LineOfText
B WRITEFILE "Names.txt", LineOfText, then INPUT LineOfText
5. Which statement is true about WRITEFILE "Names.txt", LineOfText?
A It writes the value stored in LineOfText into the file
B It writes the word LineOfText into the file

Match the Pairs

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

Left columnRight column
1WRITEFILE "Names.txt", LineOfText AWRITEFILE "Names.txt", LineOfText WRITEFILE "Names.txt", LineOfText
2LineOfText ← "Amira Khan" BThe arguments are in the wrong order. The file name must come first, then the variable holding the line of text.
3Each WRITEFILE adds one more line to the file, so two WRITEFILE statements put two lines in it CThe comma between the file name and the variable is missing.
4The line of text can come from the user: read it with INPUT into a STRING variable, then write it DStores a line of text in a STRING variable so it is ready to be written to the file
5WRITEFILE LineOfText, "Names.txt" EINPUT LineOfText WRITEFILE "Names.txt", LineOfText
6WRITEFILE "Names.txt" LineOfText FWrites the text held in the STRING variable LineOfText into Names.txt as one new line

Definitions

WRITEFILE "Names.txt", LineOfText

Fill in the Blanks

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

1.
OPENFILE "Names.txt" FOR WRITE LineOfText ← "Ava Nguyen" _________ "Names.txt", LineOfText
2.
LineOfText ← "Mia Smith" WRITEFILE "Names.txt", __________
3.
OPENFILE "Names.txt" FOR WRITE LineOfText ← "Leo Jones" WRITEFILE "_________", LineOfText
4.
OPENFILE "Names.txt" FOR WRITE _____ LineOfText WRITEFILE "Names.txt", LineOfText
5.
DECLARE LineOfText : ______ LineOfText ← "Zoe Brown" WRITEFILE "Names.txt", LineOfText

Spot the Error

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

Question 1
A DECLARE Line : INTEGER
B OPENFILE "Names.txt" FOR WRITE
C Line ← "Ava Lee"
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"
Question 2
A OPENFILE "Names.txt" FOR READ
B Line ← "Mia Chen"
C WRITEFILE "Names.txt", Line
D CLOSEFILE "Names.txt"
Question 3
A DECLARE Line : STRING
B OPENFILE "Names.txt" FOR WRITE
C Line ← "Noah Khan"
D WRITEFILE "Names.txt", "Line"
E CLOSEFILE "Names.txt"
Question 4
A DECLARE Line : STRING
B OUTPUT Line
C OPENFILE "Names.txt" FOR WRITE
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"
Question 5
A DECLARE Line : STRING
B OPENFILE "Names.txt" FOR WRITE
C Line ← Zoe Park
D WRITEFILE "Names.txt", Line
E CLOSEFILE "Names.txt"

Answer Key

WRITEFILE: writing a line of text
Section A: Quick Questions
Q1
A — WRITEFILE "Names.txt", LineOfText
Q2
B — 3 lines, one for each WRITEFILE
Q3
B — DECLARE LineOfText : STRING
Q4
A — INPUT LineOfText, then WRITEFILE "Names.txt", LineOfText
Q5
A — It writes the value stored in LineOfText into the file
Section B: Match the Pairs
Q1
WRITEFILE "Names.txt", LineOfText → Writes the text held in the STRING variable LineOfText into Names.txt as one new line
Q2
LineOfText ← "Amira Khan" → Stores a line of text in a STRING variable so it is ready to be written to the file
Q3
Each WRITEFILE adds one more line to the file, so two WRITEFILE statements put two lines in it → WRITEFILE "Names.txt", LineOfText WRITEFILE "Names.txt", LineOfText
Q4
The line of text can come from the user: read it with INPUT into a STRING variable, then write it → INPUT LineOfText WRITEFILE "Names.txt", LineOfText
Q5
WRITEFILE LineOfText, "Names.txt" → The arguments are in the wrong order. The file name must come first, then the variable holding the line of text.
Q6
WRITEFILE "Names.txt" LineOfText → The comma between the file name and the variable is missing.
Section C: Definitions
Q1
WRITEFILE "Names.txt", LineOfText — Writes the text held in the STRING variable LineOfText into the file Names.txt as one new line. Each WRITEFILE adds one more line to the file.
Section D: Fill in the Blanks
Q1
OPENFILE "Names.txt" FOR WRITE LineOfText ← "Ava Nguyen" WRITEFILE "Names.txt", LineOfText
Q2
LineOfText ← "Mia Smith" WRITEFILE "Names.txt", LineOfText
Q3
OPENFILE "Names.txt" FOR WRITE LineOfText ← "Leo Jones" WRITEFILE "Names.txt", LineOfText
Q4
OPENFILE "Names.txt" FOR WRITE INPUT LineOfText WRITEFILE "Names.txt", LineOfText
Q5
DECLARE LineOfText : STRING LineOfText ← "Zoe Brown" WRITEFILE "Names.txt", LineOfText
Section E: Spot the Error
Q1
A — Line holds a line of text, so it must be declared as STRING, not INTEGER. It should read DECLARE Line : STRING.
Q2
A — The program writes a line to the file, so the file must be opened FOR WRITE, not FOR READ. READ mode cannot be used with WRITEFILE.
Q3
D — Putting Line in quotes would write the word Line into the file. The variable name must not be in quotes so that its text (Noah Khan) is written: WRITEFILE "Names.txt", Line.
Q4
B — OUTPUT only displays a value. To take the line of text from the user and store it in Line, you must use INPUT Line.
Q5
C — A text value assigned to a STRING variable must be inside double quotes. It should be Line ← "Zoe Park".