Defining the file format isn't really an exercise in programming, it's more of a design step.
The idea is to outline how you want your file to look/act before you actually write any read/write code.
I typically create a text file and jot down all the stuff I want to save in the order in which I want to save it. It might look something like this:
char[4] header "MyFi"
s32 somedata Description of somedata
s32 blockcount Description of blockcount
{ ... for each 'blockcount' ...
s16 someval Description
s16 anotherval Description
}
|
Having this outline is useful because:
-) It gives a clear definition of what the file is supposed to look like
-) It makes it much easier to read/write the file, since all you have to do is follow the outline
-) If you have problems reading/writing the file, it makes them easier to debug because you can look at the file in a hex editor and see exactly how the data is being stored. You can compare that to your outline to make sure it's getting stored correctly.
If you look at documentation for any published binary format, they all do something similar to this. They outline the format in great detail.
For a real-life example... see the zip file format in appnote:
https://www.pkware.com/documents/casestudies/APPNOTE.TXT
(skip to section 4.3.6 where it actually outline the format)
Of course... your outline does not have to be
that detailed and verbose. But it does certainly help to lay everything out.