[comp.lang.c] Using binary data files

conor@lion.inmos.co.uk (Conor O'Neill) (03/27/91)

In article <1991Mar21.021023.25615@athena.mit.edu> scs@adam.mit.edu writes:
>The moral: DON'T USE BINARY DATA FILES.  (You'll say, "but we
>have to, for efficiency."  Sure, and go ahead, but don't come
>crying to me :-) when you have problems -- and you will have
>problems.)

Binary data files are fine, AS LONG AS YOU SPECIFY THE FORMAT,
in the same way that if you use an ASCII file, you would say something
like "Ten decimal integers separated by spaces, on each line".
The problem is that many people `document' the format by supplying a
struct definition, and, of course, struct definitions are most definitely
_not_ portable.

A binary file specified as something like "Each 'record' is a list of 10
32-bit twos-complement integers, each stored least significant byte first"
is perfectly portable. On some machines, they could be read directly into
a struct. On others, simple reading and writing routines would be required.
But, of course, this would be in the machine-dependent part of your program,
together with all the other machine-dependent parts which would have
to be modified anyway when porting.

---
Conor O'Neill, Software Group, INMOS Ltd., UK.
UK: conor@inmos.co.uk		US: conor@inmos.com
"It's state-of-the-art" "But it doesn't work!" "That is the state-of-the-art".

bhoughto@hopi.intel.com (Blair P. Houghton) (03/29/91)

In article <15086@ganymede.inmos.co.uk> conor@inmos.co.uk (Conor O'Neill) writes:
>In article <1991Mar21.021023.25615@athena.mit.edu> scs@adam.mit.edu writes:
>Binary data files are fine, AS LONG AS YOU SPECIFY THE FORMAT,
>The problem is that many people `document' the format by supplying a
>struct definition, and, of course, struct definitions are most definitely
>_not_ portable.
>A binary file specified as something like "Each 'record' is a list of 10
>32-bit twos-complement integers, each stored least significant byte first"
>is perfectly portable. On some machines, they could be read directly into

But, of course, if you're the person who _should_ be
doing the port, you'll be familiar with both architectures,
and will know how to translate one struct to the other,
bit-by-bit, if necessary.

The rule to follow about binary files is: if it wasn't written
on this machine under this OS, don't expect it to work, just
try it and see if it does.  It's almost guaranteed to be a
problem needing hacking, and expecting that expense will do
much more for your stressor-count than anyone's bitwise
struct definitions.

				--Blair
				  "I need to dig a posthole and I
				   don't want to reinvent the wheel,
				   so could someone please email me
				   the plans for one?  In ANSI format?"