Cambridge A2 Level Computer Science 9618 · 20.2 File Processing and Exception Handling

Q25 · 9618/31/M/J/23 Q10

Worked solution from the Further Programming Paper 3 Advanced Theory booklet, Part 2 of 2.

Q259618/31/M/J/23original Q10Structured · 5 marks
Question 10 from 9618/31/M/J/23

Worked solution

Open the active file for reading, loop until its end, write the right text, then close both files.

DECLARE Account : STRING
OPENFILE "ActiveFile.txt" FOR READ
OPENFILE "ArchiveFile.txt" FOR WRITE
WHILE NOT EOF("ActiveFile.txt")
    READFILE "ActiveFile.txt", Account
    IF Account = "" THEN
        WRITEFILE "ArchiveFile.txt", "Account not present"
    ELSE
        WRITEFILE "ArchiveFile.txt", Account
    ENDIF
ENDWHILE
CLOSEFILE "ActiveFile.txt"
CLOSEFILE "ArchiveFile.txt"

OPENFILE "ActiveFile.txt" FOR READ [1]

EOF("ActiveFile.txt") as the loop condition [1]

"Account not present" written for a blank line [1]

Account written in the ELSE part [1]

CLOSEFILE "ActiveFile.txt" after the loop [1]

KEY IDEAOpen each file with the right mode, loop until EOF, read and write one line at a time, then close every file.
TOTAL 5 marks

Official mark scheme

Mark scheme for 9618/31/M/J/23 question 10
VIDEO
▶ Whole paper

Video solution of the whole paper · by qiphyedu. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.