Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
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.
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]
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]
▶ 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.