← IGCSE Computer Science Paper 2

Copying a line of text from one file to another

Writes the line held in LineOfText (read earlier from FileA.txt) into FileB.txt, copying it between files. Both files must be opened first and each one closed after.

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 500–522) and word search (Modules 500–538).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line opens FileA.txt so a line of text can be copied from it?
OPENFILE FileA.txt FOR READOPENFILE FileA.txt FOR WRITE
Matches
OPENFILE FileA.txt FOR WRITE READFILE FileA.txt, LineOfText
↓
FileA.txt is opened for WRITE but then read from. A file you read from must be opened FOR READ, and WRITE would also replace its data.
Applying
Meanings
Term
WRITEFILE FileB.txt, LineOfText
Meaning
Writes the line held in LineOfText (read earlier from FileA.txt) into FileB.txt, copying it between files. Both files must be opened first and each one closed after.
Practice
DECLARE LineOfText : STRING
Testing
Gaps
OPENFILE FileA.txt FOR
Check Answer
Spots
1OPENFILE FileA.txt FOR WRITE
2OPENFILE FileB.txt FOR WRITE

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Bear' badge — Forest Guardian, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

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.

Match the Pairs

OPENFILE FileB.txt FOR WRITE → Opens the second file in WRITE mode so the program can put the copied line into it, while FileA.txt is already open for READ
READFILE FileA.txt, LineOfText → Reads one line from FileA.txt and stores it in the variable LineOfText, which carries the line to the other file
The copied line is written into the second file using the same variable that READFILE filled → WRITEFILE FileB.txt, LineOfText
Each file that was opened must be closed once the copying is finished, so both files need a CLOSEFILE → CLOSEFILE FileA.txt CLOSEFILE FileB.txt
OPENFILE FileA.txt FOR WRITE READFILE FileA.txt, LineOfText → FileA.txt is opened for WRITE but then read from. A file you read from must be opened FOR READ, and WRITE would also replace its data.
READFILE FileA.txt, LineOfText WRITEFILE FileB.txt, LineOfText CLOSEFILE FileA.txt → FileB.txt is never closed. Both files need CLOSEFILE, so CLOSEFILE FileB.txt is missing.

Key Term

WRITEFILE FileB.txt, LineOfText
Writes the line held in LineOfText (read earlier from FileA.txt) into FileB.txt, copying it between files. Both files must be opened first and each one closed after.

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.

Student with a class code? Join your class →