On Thu, Mar 03, 2022 at 08:27:50AM -0000, Kevin Mills wrote:
The code I'm currently working on involves parsing binary data. If I ask for, say, 4 bytes, it's because I actually need 4 bytes and if the file doesn't have 4 bytes for me, it's malformed. Because `f.read(4)` can silently return less than 4 bytes and I don't want to have to explicitly double check every read, I'm using a wrapper function.
This is not a terrible idea. Other languages, like Pascal, have facility for reading fixed-size chunks from a file, reading integers or floats from a file. But there are some design questions that need to be sorted out. If you're reading from, say, a serial port, and it only has three bytes, should it hang until a fourth byte is available, or raise an exception (thus losing the first three bytes)? -- Steve