Copying a line of text from one file to another

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

Copying a line of text from one file to another

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
3. Which statement reads one line from FileA.txt into the variable LineOfText?
A READFILE FileA.txt, LineOfText
B READFILE FileB.txt, LineOfText
4. Which statement copies the line held in LineOfText into FileB.txt?
A WRITEFILE FileB.txt, LineOfText
B WRITEFILE FileA.txt, LineOfText
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

Match the Pairs

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

Left columnRight column
1OPENFILE FileB.txt FOR WRITE AReads one line from FileA.txt and stores it in the variable LineOfText, which carries the line to the other file
2READFILE FileA.txt, LineOfText BFileA.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.
3The copied line is written into the second file using the same variable that READFILE filled CWRITEFILE FileB.txt, LineOfText
4Each file that was opened must be closed once the copying is finished, so both files need a CLOSEFILE DCLOSEFILE FileA.txt CLOSEFILE FileB.txt
5OPENFILE FileA.txt FOR WRITE READFILE FileA.txt, LineOfText EOpens the second file in WRITE mode so the program can put the copied line into it, while FileA.txt is already open for READ
6READFILE FileA.txt, LineOfText WRITEFILE FileB.txt, LineOfText CLOSEFILE FileA.txt FFileB.txt is never closed. Both files need CLOSEFILE, so CLOSEFILE FileB.txt is missing.

Definitions

WRITEFILE FileB.txt, LineOfText

Fill in the Blanks

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

1.
OPENFILE FileA.txt FOR ____ OPENFILE FileB.txt FOR WRITE
2.
READFILE FileA.txt, LineOfText _________ FileB.txt, LineOfText
3.
READFILE FileA.txt, __________ WRITEFILE FileB.txt, LineOfText
4.
WRITEFILE FileB.txt, LineOfText CLOSEFILE FileA.txt _________ FileB.txt
5.
OPENFILE FileB.txt FOR WRITE WRITEFILE _________, LineOfText

Spot the Error

Shade in the letter of the line that contains an 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
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
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
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
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

Answer Key

Copying a line of text from one file to another
Section A: Quick Questions
Q1
B — OPENFILE FileA.txt FOR READ
Q2
B — OPENFILE FileB.txt FOR WRITE
Q3
A — READFILE FileA.txt, LineOfText
Q4
A — WRITEFILE FileB.txt, LineOfText
Q5
A — Two: FileA.txt in READ mode and FileB.txt in WRITE mode
Section B: Match the Pairs
Q1
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
Q2
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
Q3
The copied line is written into the second file using the same variable that READFILE filled → WRITEFILE FileB.txt, LineOfText
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
OPENFILE FileA.txt FOR READ OPENFILE FileB.txt FOR WRITE
Q2
READFILE FileA.txt, LineOfText WRITEFILE FileB.txt, LineOfText
Q3
READFILE FileA.txt, LineOfText WRITEFILE FileB.txt, LineOfText
Q4
WRITEFILE FileB.txt, LineOfText CLOSEFILE FileA.txt CLOSEFILE FileB.txt
Q5
OPENFILE FileB.txt FOR WRITE WRITEFILE FileB.txt, LineOfText
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
B — The OPENFILE statement needs the keyword FOR before the mode. It should be OPENFILE FileA.txt FOR READ.
Q4
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.
Q5
B — There must be a comma between the file name and the variable in READFILE. It should be READFILE FileA.txt, TextLine.