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

Q31 · 9618/33/M/J/25 Q11

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

Q319618/33/M/J/25original Q11Structured · 5 marks
Question 11 from 9618/33/M/J/25

Worked solution

Open the file first. In the loop, SEEK to the location, read the record, and write the new item if the location is empty. After the loop, report if nothing was stored.

OPENFILE "StockList.dat" FOR RANDOM
OUTPUT "Enter the new item you wish to store: "
INPUT NewStock
WHILE NOT Stored AND Location <= Max
    SEEK "StockList.dat", Location
    GETRECORD "StockList.dat", CurrentStock
    IF CurrentStock = "" THEN
        PUTRECORD "StockList.dat", NewStock
        Stored ← TRUE
    ELSE
        Location ← Location + 1
    ENDIF
ENDWHILE
IF Stored = FALSE THEN
    OUTPUT "The new item has not been stored as the file was full."
ENDIF
CLOSEFILE "StockList.dat"

OPENFILE "StockList.dat" FOR RANDOM [1]

SEEK "StockList.dat", Location [1]

GETRECORD "StockList.dat", CurrentStock [1]

PUTRECORD "StockList.dat", NewStock [1]

IF Stored = FALSE THEN (NOT Stored is also fine) [1]

KEY IDEAOpen the random file, SEEK then GETRECORD to test each location, and PUTRECORD at the first empty one.
TOTAL 5 marks

Official mark scheme

Mark scheme for 9618/33/M/J/25 question 11
VIDEO
▶ Topic lesson

A lesson on this topic · by James Gan. 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.