Computational Thinking and Data Structures: Question 7

Syllabus 10.3

Structured AS 9 marks

A school library records the titles of books that are currently overdue in a text file called "Overdue.txt", which already contains one book title per line. Assume the variable BookTitle has already been assigned the title of a newly identified overdue book, as a string.

(a) Write Cambridge pseudocode to open "Overdue.txt" so that a new line can be added to the end of the file without erasing any of the lines already stored in it, write the value of BookTitle to a new line in the file, and then close the file. [3]

(b) Write Cambridge pseudocode, using a WHILE loop and the EOF function, to open "Overdue.txt" for reading, read every line from the file in turn, count how many lines (book titles) the file contains, output this count once every line has been read, and then close the file. [5]

(c) State one reason why a program should close a text file once it has finished reading from or writing to it. [1]

Show worked solution Hide worked solution

Worked solution

Part (a): Adding a new line without erasing existing data

“Overdue.txt” already contains data that must not be lost, so the file must be opened in a mode that adds new data to the end of the file rather than starting the file fresh. In Cambridge pseudocode this is the APPEND file mode:

OPENFILE "Overdue.txt" FOR APPEND
WRITEFILE "Overdue.txt", BookTitle
CLOSEFILE "Overdue.txt"

Opening the file FOR WRITE instead would erase every title already stored in “Overdue.txt” before writing the new one, which is not what is wanted here.

[3 marks]: [1] for OPENFILE "Overdue.txt" FOR APPEND (not FOR WRITE), [1] for WRITEFILE "Overdue.txt", BookTitle (writing the value stored in BookTitle, not the literal text BookTitle), [1] for CLOSEFILE "Overdue.txt".

Part (b): Reading every line and counting them

To read every line of an existing text file without knowing in advance how many lines it contains, a pre-condition WHILE loop is used together with the EOF (end-of-file) function, which returns TRUE once every line has already been read:

DECLARE Line : STRING
DECLARE LineCount : INTEGER

LineCount ← 0
OPENFILE "Overdue.txt" FOR READ
WHILE NOT EOF("Overdue.txt")
    READFILE "Overdue.txt", Line
    LineCount ← LineCount + 1
ENDWHILE
OUTPUT LineCount
CLOSEFILE "Overdue.txt"

LineCount must be set to 0 before the loop starts, so that it correctly counts up from zero. The file is opened FOR READ (not FOR APPEND or FOR WRITE, which would not allow existing lines to be read back). Each pass of the loop reads exactly one line into Line with READFILE and then increments LineCount by 1. Once EOF("Overdue.txt") becomes TRUE. Meaning there is no line left to read. The loop condition NOT EOF("Overdue.txt") becomes FALSE and the loop stops, after which the final count is output and the file is closed.

[5 marks]: [1] for LineCount ← 0 before the loop, [1] for OPENFILE "Overdue.txt" FOR READ, [1] for the loop condition WHILE NOT EOF("Overdue.txt"), [1] for READFILE "Overdue.txt", Line inside the loop paired with LineCount ← LineCount + 1, [1] for OUTPUT LineCount and CLOSEFILE "Overdue.txt" after the loop ends.

Part (c): Why a file should be closed

Closing a text file once a program has finished reading from or writing to it ensures that any data written to the file is properly saved (flushed) to permanent storage, and it releases the file so that it is available for other programs, or other parts of the same program, to open and use. [1 mark] for any one valid reason along these lines.

Final answers

  • (a) OPENFILE "Overdue.txt" FOR APPEND / WRITEFILE "Overdue.txt", BookTitle / CLOSEFILE "Overdue.txt"
  • (b) See the pseudocode above: LineCount ← 0, OPENFILE "Overdue.txt" FOR READ, a WHILE NOT EOF("Overdue.txt") loop reading each line and incrementing LineCount, then OUTPUT LineCount and CLOSEFILE "Overdue.txt"
  • (c) Closing a file saves any data written to it and frees the file for other use (or another valid reason)