Writing a complete file handling program in 0478 pseudocode

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

Writing a complete file handling program in 0478 pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which shows the correct order of a complete file-writing program?
A OPENFILE, WRITEFILE, DECLARE, CLOSEFILE
B DECLARE, OPENFILE, WRITEFILE, CLOSEFILE
2. Which line opens a file so a program can save data to it?
A OPENFILE "scores.txt" FOR WRITE
B OPENFILE scores.txt FOR WRITE
3. Which is the better identifier for a variable holding a player's name that will be saved to a file?
A x
B PlayerName
4. In 0478 pseudocode, how should keywords such as DECLARE and OPENFILE be written?
A In capital letters
B In lower case
5. Where should the CLOSEFILE statement go in a complete file program?
A After the last read or write
B Straight after DECLARE

Match the Pairs

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

Left columnRight column
1DECLARE Name : STRING AStep 1: declare a variable to hold the data that will be written to or read from the file.
2CLOSEFILE("Scores.txt") BOPENFILE "Names.txt" FOR WRITE
3After declaring variables, open the file in WRITE mode so data can be stored in it CThe final step: close the file once all the reading or writing is finished so the data is safely saved.
4A comment explains the purpose of the file, and keywords are in capitals D// Store the high scores for the quiz game
5OPENFILE "Names.txt" FOR WRITE DECLARE Name : STRING WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt" EThe statements are in the wrong order. OPENFILE comes before DECLARE, but the variable should be declared first.
6DECLARE Name : STRING OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name FThe program never closes the file. CLOSEFILE "Names.txt" is missing from the end.

Definitions

DECLARE, OPENFILE, WRITEFILE, CLOSEFILE

Fill in the Blanks

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

1.
_______ PlayerName : STRING OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName
2.
DECLARE PlayerName : STRING OPENFILE "Players.txt" FOR _____ WRITEFILE "Players.txt", PlayerName
3.
OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName _________ "Players.txt"
4.
OPENFILE "Players.txt" FOR READ ________ "Players.txt", PlayerName CLOSEFILE "Players.txt"
5.
DECLARE Score : INTEGER OPENFILE "Score.txt" FOR WRITE WRITEFILE "Score.txt", _____

Spot the Error

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

Question 1
A DECLARE Name : STRING
B OPENFILE "names.txt" FOR READ
C WRITEFILE "names.txt", Name
D CLOSEFILE "names.txt"
Question 2
A DECLARE Line : STRING
B OPENFILE FOR READ
C READFILE "diary.txt", Line
D OUTPUT Line
E CLOSEFILE "diary.txt"
Question 3
A DECLARE Score : INTEGER
B OPENFILE "score.txt" FOR WRITE
C WRITEFILE Score, "score.txt"
D CLOSEFILE "score.txt"
Question 4
A DECLARE Word : STRING
B OPENFILE "words.txt" FOR READ
C READFILE "words.txt"
D OUTPUT Word
E CLOSEFILE "words.txt"
Question 5
A // Save the pupil's name
B DECLARE Pupil STRING
C OPENFILE "pupil.txt" FOR WRITE
D WRITEFILE "pupil.txt", Pupil
E CLOSEFILE "pupil.txt"

Answer Key

Writing a complete file handling program in 0478 pseudocode
Section A: Quick Questions
Q1
B — DECLARE, OPENFILE, WRITEFILE, CLOSEFILE
Q2
A — OPENFILE "scores.txt" FOR WRITE
Q3
B — PlayerName
Q4
A — In capital letters
Q5
A — After the last read or write
Section B: Match the Pairs
Q1
DECLARE Name : STRING → Step 1: declare a variable to hold the data that will be written to or read from the file.
Q2
CLOSEFILE("Scores.txt") → The final step: close the file once all the reading or writing is finished so the data is safely saved.
Q3
After declaring variables, open the file in WRITE mode so data can be stored in it → OPENFILE "Names.txt" FOR WRITE
Q4
A comment explains the purpose of the file, and keywords are in capitals → // Store the high scores for the quiz game
Q5
OPENFILE "Names.txt" FOR WRITE DECLARE Name : STRING WRITEFILE "Names.txt", Name CLOSEFILE "Names.txt" → The statements are in the wrong order. OPENFILE comes before DECLARE, but the variable should be declared first.
Q6
DECLARE Name : STRING OPENFILE "Names.txt" FOR WRITE WRITEFILE "Names.txt", Name → The program never closes the file. CLOSEFILE "Names.txt" is missing from the end.
Section C: Definitions
Q1
DECLARE, OPENFILE, WRITEFILE, CLOSEFILE — The order of a complete file program: declare variables, open the file in the right mode, read or write the data, then close the file. Keywords are in capitals and statements are indented.
Section D: Fill in the Blanks
Q1
DECLARE PlayerName : STRING OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName
Q2
DECLARE PlayerName : STRING OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName
Q3
OPENFILE "Players.txt" FOR WRITE WRITEFILE "Players.txt", PlayerName CLOSEFILE "Players.txt"
Q4
OPENFILE "Players.txt" FOR READ READFILE "Players.txt", PlayerName CLOSEFILE "Players.txt"
Q5
DECLARE Score : INTEGER OPENFILE "Score.txt" FOR WRITE WRITEFILE "Score.txt", Score
Section E: Spot the Error
Q1
B — The program writes data with WRITEFILE, so the file must be opened in WRITE mode. Line 2 should be OPENFILE "names.txt" FOR WRITE.
Q2
B — OPENFILE needs the file identifier (the file name as a string) before the mode. It should be OPENFILE "diary.txt" FOR READ.
Q3
C — WRITEFILE takes the file name first, then a comma, then the data. It should be WRITEFILE "score.txt", Score.
Q4
C — READFILE needs a variable to store the data it reads. It should be READFILE "words.txt", Word.
Q5
B — A DECLARE statement needs a colon between the identifier and the data type. It should be DECLARE Pupil : STRING.