Worked solution from the Further Programming Paper 3 Advanced Theory booklet, Part 2 of 2.
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]
▶ 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.