Understanding Copying a line of text from one file to another in IGCSE Computer Science Paper 2
What it does: WRITEFILE puts the contents of LineOfText, read earlier from FileA.txt, into FileB.txt. Every opened file needs its own CLOSEFILE statement, so two files mean two CLOSEFILE lines. The source file is read from, so it is opened FOR READ. Opening it FOR WRITE would replace its data. One variable holds the line between the READFILE and the WRITEFILE, so the line can be copied.
Common mistakes: FileA.txt is the source file, so it must be opened FOR READ. Opening it FOR WRITE would wipe its contents and stop READFILE from working. It should be OPENFILE FileA.txt FOR READ. The line must be written to the destination file, FileB.txt. FileA.txt is open for READ, so it cannot be written to. It should be WRITEFILE FileB.txt, TextLine.
Quick Questions
1. Which line opens FileA.txt so a line of text can be copied from it?
A. OPENFILE FileA.txt FOR WRITE
B. OPENFILE FileA.txt FOR READ ✓
The source file is read from, so it is opened FOR READ. Opening it FOR WRITE would replace its data.
2. Which line opens FileB.txt so the copied line can be stored in it?
A. OPENFILE FileB.txt FOR READ
B. OPENFILE FileB.txt FOR WRITE ✓
The destination file receives data, so it is opened FOR WRITE.
3. Which statement reads one line from FileA.txt into the variable LineOfText?
A. READFILE FileA.txt, LineOfText ✓
B. READFILE FileB.txt, LineOfText
READFILE must name the file being read from. In this copy, that is FileA.txt.
4. Which statement copies the line held in LineOfText into FileB.txt?
A. WRITEFILE FileB.txt, LineOfText ✓
B. WRITEFILE FileA.txt, LineOfText
WRITEFILE names the destination file, FileB.txt. The variable LineOfText carries the line between the two files.
5. How many files are open at the same time in the FileA.txt to FileB.txt copy?
A. Two: FileA.txt in READ mode and FileB.txt in WRITE mode ✓
B. One: both files share a single OPENFILE statement
Each file needs its own OPENFILE statement, and the two can use different modes at once.
Fill in the Blanks
1. OPENFILE FileA.txt FOR READ
OPENFILE FileB.txt FOR WRITE
Hint: Type the file mode needed for the source file, the one the line is copied from.
2. READFILE FileA.txt, LineOfText
WRITEFILE FileB.txt, LineOfText
Hint: Type the command that puts the stored line into the destination file.
3. READFILE FileA.txt, LineOfText
WRITEFILE FileB.txt, LineOfText
Hint: Type the variable that holds the line while it travels between the two files.
4. WRITEFILE FileB.txt, LineOfText
CLOSEFILE FileA.txt
CLOSEFILE FileB.txt
Hint: Type the command that finishes with the second file, just as the first one was closed.
5. OPENFILE FileB.txt FOR WRITE
WRITEFILE FileB.txt, LineOfText
Hint: Type the file identifier of the destination file that was opened for writing.
Spot the Error
Question 1
A
OPENFILE FileA.txt FOR WRITE
B
OPENFILE FileB.txt FOR WRITE
C
READFILE FileA.txt, TextLine
D
WRITEFILE FileB.txt, TextLine
E
CLOSEFILE FileA.txt
F
CLOSEFILE FileB.txt
Line A — FileA.txt is the source file, so it must be opened FOR READ. Opening it FOR WRITE would wipe its contents and stop READFILE from working. It should be OPENFILE FileA.txt FOR READ.
Question 2
A
OPENFILE FileA.txt FOR READ
B
OPENFILE FileB.txt FOR WRITE
C
READFILE FileA.txt, TextLine
D
WRITEFILE FileA.txt, TextLine
E
CLOSEFILE FileA.txt
F
CLOSEFILE FileB.txt
Line D — The line must be written to the destination file, FileB.txt. FileA.txt is open for READ, so it cannot be written to. It should be WRITEFILE FileB.txt, TextLine.
Question 3
A
OPENFILE FileB.txt FOR WRITE
B
OPENFILE FileA.txt READ
C
READFILE FileA.txt, TextLine
D
WRITEFILE FileB.txt, TextLine
E
CLOSEFILE FileA.txt
F
CLOSEFILE FileB.txt
Line B — The OPENFILE statement needs the keyword FOR before the mode. It should be OPENFILE FileA.txt FOR READ.
Question 4
A
OPENFILE FileA.txt FOR READ
B
OPENFILE FileB.txt FOR WRITE
C
READFILE TextLine, FileA.txt
D
WRITEFILE FileB.txt, TextLine
E
CLOSEFILE FileA.txt
F
CLOSEFILE FileB.txt
Line C — READFILE takes the file name first, then the variable that stores the line. The arguments are in the wrong order. It should be READFILE FileA.txt, TextLine.
Question 5
A
OPENFILE FileA.txt FOR READ
B
READFILE FileA.txt TextLine
C
OPENFILE FileB.txt FOR WRITE
D
WRITEFILE FileB.txt, TextLine
E
CLOSEFILE FileA.txt
F
CLOSEFILE FileB.txt
Line B — There must be a comma between the file name and the variable in READFILE. It should be READFILE FileA.txt, TextLine.