Binary data file compatibility across machines

Al Dunbar userAKDU at mts.ucs.UAlberta.CA
Thu Nov 29 13:39:21 AEST 1990


In article <2172 at tuvie>, hp at vmars.tuwien.ac.at (Peter Holzer) writes:
>stiber at cs.ucla.edu (Michael D Stiber) writes:
>
>
>>On different machines, the implementation of C data types is different.
<<<deletions>>>
>
>For integer data I choose the format that is used on the machines
>I am working on most of the time. Each binary data file then
>gets a header describing the data format. Something like
>
><magic number>          2 Bytes: 'P' 'B' (portable binary)
><integer type>          1 Byte: 0 = 2compl., 1 = 1compl.,
>                                2 = sign/mag,
><endianness>            1 Byte: 0 = little, 1 = big.
><float-format>          ??
>
Pardon my curiosity, but, if you write such a file on a
particular type of machine, then read it back on another,
won't your code have to do some decoding of this header
information? Say, for example, you write from an ASCII
machine and read from an EBCDIC one. The "PB" will map to
some other combination of characters. Will your program
determine from whatever they happen to be that the source
machine is ASCII? I always forget whether "endianness"
refers to the ordering of bytes in words or bits in bytes -
if the latter, your program will also have to do some
juggling to properly decode the third and fourth bytes.
What about machine architectures you don't know about yet?
What about 12 and 60 bit machines (PDP8, Cyber)?
 
If transportability is important, use ASCII (pardon, character),
and let some o/s utility do the conversion. If efficiency is
paramount, use binary and include a disclaimer about moving
the file to another machine (you can't, after all, move the
executable that way, can you?). If both are crucial, provide
a separate conversion program.
 
-------------------+-------------------------------------------
Al Dunbar          |
Edmonton, Alberta  |  "this mind left intentionally blank"
CANADA             |          - Manuel Writer
-------------------+-------------------------------------------
#! r



More information about the Comp.lang.c mailing list