Reading Fixed-Width Files

Another way of processing files without consuming them whole is to consume a fixed number of bytes. It’s much like the streaming examples that we’ve seen, where we read from the file until we encountered a newline or until we encountered a comma. But instead of reading until we hit a particular character, we read, say, ten bytes from the current position, receiving a string containing those ten bytes.

This might seem an inflexible and impractical way of doing things. After all, how can we know at how many bytes from the start of the file we’ll find the information we want? But this sort of processing has several real-world applications and has an important performance characteristic that gives it many of the benefits ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.