Data structures · A level · AQA 7517 4.2.1.3, Eduqas A500QS 2.4 · about 25 min
Text and binary files, fixed-length records, and serial, sequential, indexed sequential and direct access files.
[1 mark]What is the difference between a serial file and a sequential file?
[1 mark]Which file organisations let a program go straight to, or near, a record without reading from the start of the file?
Tick every answer that is true.
[1 mark]A file holds fixed-length records of 32 bytes each, starting at byte 0. At which byte does record number 5 start, counting records from 0?
[1 mark]An airline's booking system looks up and changes one seat record at a time as customers call. Which file organisation suits it best?
[1 mark]Put the steps for updating a sequential master file from a transaction file in order.
Number the lines 1 to 5 to put them in the right order.
When the keys match, apply the transaction and write the updated recordSort the transaction file into the same key order as the master fileWhen one file runs out, copy the rest of the other fileWrite whichever current record has the smaller key to the new master file, and read the next from that fileRead the first record from each fileSort the transaction file into the same key order as the master file Read the first record from each file Write whichever current record has the smaller key to the new master file, and read the next from that file When the keys match, apply the transaction and write the updated record When one file runs out, copy the rest of the other file
Both files must be in key order so a single merge pass can compare them record by record.
[1 mark]What is an advantage of a binary file over a text file for storing records?
master.txt is a sequential master file of robots, in ascending order of id, with the header id,name,best_cm. results.txt is a transaction file with the header id,name,cm, also in ascending id order, holding at most one new run per robot. Both files are shown below. Merge them in one pass, without sorting, into a new file new_master.txt:
- The first line is the header id,name,best_cm.
- Then one line per robot, in ascending id order, in the form id,name,best_cm.
- A robot in master.txt with no result is copied unchanged.
- A robot in both files keeps the larger distance, written exactly as it appears in whichever file it came from (for example 41.0).
- A robot only in results.txt is added with its cm as its best_cm.
Finally print <improved> records improved, <added> added, where improved counts robots whose best distance went up and added counts new robots. The robot does not move.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
with open("master.txt") as f:
master = f.read().splitlines()
with open("results.txt") as f:
results = f.read().splitlines()The hint students can ask for: Keep one position in each file. Compare the two current ids: the smaller one is written first and only its file moves on. When the ids match, write one record with the better distance and move both on. When one file runs out, copy the rest of the other.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
with open("master.txt") as f:
master = f.read().splitlines()
with open("results.txt") as f:
results = f.read().splitlines()
out = open("new_master.txt", "w")
out.write(master[0] + "\n")
m, t = 1, 1
improved, added = 0, 0
while m < len(master) or t < len(results):
if t >= len(results):
out.write(master[m] + "\n")
m = m + 1
continue
if m >= len(master):
out.write(results[t] + "\n")
added = added + 1
t = t + 1
continue
mid, mname, mbest = master[m].split(",")
tid, tname, tcm = results[t].split(",")
if int(mid) < int(tid):
out.write(master[m] + "\n")
m = m + 1
elif int(tid) < int(mid):
out.write(results[t] + "\n")
added = added + 1
t = t + 1
else:
if float(tcm) > float(mbest):
out.write(f"{mid},{mname},{tcm}\n")
improved = improved + 1
else:
out.write(master[m] + "\n")
m = m + 1
t = t + 1
out.close()
print(f"{improved} records improved, {added} added")
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.