So far in this chapter, we've looked at reading and writing text-based data formats. This will allow you to work with many types of data (and to easily create your own formats), but it's only half of the picture. In this example, we'll look at the other half-binary formats.
In this example, we'll be writing out a binary version of our FOO file. We'll call such files FOB (foo, binary). As was the case with FOO files, FOB files are a scaled-down example of the types of things that are often encountered in real formats. FOB files will contain the same data we saw in FOO files, namely a list of object types and positions, but stored in a way common to binary formats.
Most binary files consist of two main parts:
A header, which is a fixed-size chunk that describes the nature of the rest of the document.
Entries that are read according to the data layout specified in the header.
In the case of our FOB files, our header will contain the following:
An integer (1 byte)...