Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Open the file for reading, read one line at a time until the end, build each message from STX, destination, source, data and ETX, and send it. After the loop send the final "**" message.
PROCEDURE SendFile(FileName : STRING, DestID : STRING, Port : INTEGER) DECLARE FileData : STRING CONSTANT STX = CHR(02) CONSTANT ETX = CHR(03) OPENFILE FileName FOR READ WHILE NOT EOF(FileName) READFILE FileName, FileData FileData ← STX & DestID & MyID & FileData & ETX CALL Transmit(FileData, Port) ENDWHILE CLOSEFILE FileName CALL Transmit(STX & DestID & MyID & "**" & ETX, Port) ENDPROCEDURE
File opened in READ mode using the parameter, and closed afterwards [1]
Conditional loop that stops at EOF [1]
READFILE used to get one line [1]
An attempt to form a message from the destination ID, MyID and the line [1]
Message formed completely correctly (STX, DestID, MyID, data, ETX in order) [1]
Transmit() called with the message and the port, inside the loop [1]
Final message with data "**" and all its parts sent after the loop [1]
Write any two of these three points.
It shows that all the lines of the file have been sent, so it is the end of the transfer [1]
So the receiving program knows to stop waiting for more data [1]
Other points that also score: the receiving program can close or save the file.
A message must contain at least one character of data, so a zero-length data field is not allowed [1]
This means a blank line cannot be sent [1]
Add a special character to the start of the message text (or add an extra indicator field to the message format) [1]
The receiver reads this extra character or field as meaning "this line is blank" [1]
Use CASE to pick the field. The DestinationID is 3 characters after STX, the SourceID follows it, and the Data is the rest apart from the final ETX. Return an empty string for any other number.
FUNCTION GetField(Msg : STRING, FieldNo : INTEGER) RETURNS STRING DECLARE RetString : STRING CASE OF FieldNo 1 : RetString ← MID(Msg, 2, 3) 2 : RetString ← MID(Msg, 5, 3) 3 : RetString ← MID(Msg, 8, LENGTH(Msg) - 8) OTHERWISE : RetString ← "" ENDCASE RETURN RetString ENDFUNCTION
Selection with CASE or IF to choose the field [1]
Fields 1 and 2 extracted correctly [1]
Length of field 3 worked out (the message length minus the STX, the two IDs and the ETX) [1]
Field 3 extracted correctly [1]
Empty string returned for an invalid field number, using OTHERWISE or by starting with an empty string [1]
A final RETURN of the result [1]
▶ Starts at Q8 (48:04)
Starts at Q8 (48:04) · by Pseudocode Pro. 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.