Cambridge AS Level Computer Science 9618 · 10.3 Files

Q66 · 9618/22/O/N/25 Q3

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.

Q669618/22/O/N/25original Q3Structured · 10 marks
Question 3 from 9618/22/O/N/25

Worked solution

(a)

Complete each step in order:

Step 1: open the file OldFile.txt in read mode [1]

Step 2: open the file NewFile.txt in write mode [1]

Step 3: read a line from OldFile.txt and store in LineX

Step 4: read a line from OldFile.txt and store in LineY

Step 5: read a line from OldFile.txt and store in LineZ. Steps 3 to 5 read all three items from OldFile.txt into the correct variables [1]

Step 6: set NewString to LineX & '\' & LineY & '\' & LineZ [1]

Step 7: write NewString to NewFile.txt [1]

Step 8: repeat from step 3 until end of OldFile.txt [1]

Step 9: close both files.

(b)

The character '\' does not appear in the data items (the ID, name or email address), so it cannot be mistaken for part of the data. [1]

(c)

The new items can contain '\', so '\' cannot mark where they end. Use their lengths instead.

Add a new separator after the third (original) item, so the program knows where the new items start [1]

Work out the length of each new item and store the lengths with the line [1]

Store each length as a fixed-length digit string (two digits as the maximum is 99), so the program reads the length first and then reads exactly that many characters [1]

KEY IDEAJoin items with a separator that never appears in the data; if data can contain any character, store the length of the item instead.
TOTAL 10 marks

Official mark scheme

Mark scheme for 9618/22/O/N/25 question 3
VIDEO
▶ Whole paper

Video solution of the whole paper · by MAK CS. 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.