Binary Files · 12 / 14 Exercise Hard

Binary records with struct

Objective

Binary record files store each field in a fixed size. struct converts Python values to bytes and back according to a format.

Instructions

  • The first line has the number n of records; each one is a line name age score (name of up to 10 ASCII letters, age from 0 to 255 and score from 0 to 65535).
  • Save the records in registros.bin with the format "<10sBH" (little-endian: 10-byte text, one byte and an unsigned 2-byte integer). Show Record size: 13 and then read the file record by record and show name age score per line (the name without the null padding bytes).
Input
2
ana 30 950
luis 7 65535
Output
Record size: 13
ana 30 950
luis 7 65535

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise