Mail Archives: djgpp/1996/04/13/01:47:43
Joe Smith (jes AT presto DOT med DOT upenn DOT edu) wrote:
: In article <4khbdr$1on AT vidar DOT diku DOT dk> terra AT diku DOT dk (Morten Welinder) writes:
[snip]
: Sigh. Structures as file headers: just say 'no'.
Definitely! You may be able to outsmart one compiler, but another one
will/may do it differently. Also, the order of bitfields varies. Some
start packing from the most significant bit, others from the least.
Moving between machines, byte order varies. There are 24 (4 factorial)
ways to store a 4-byte integer, 17 of them are in use by somebody.
For max portability, read into a byte array and convert to the struct
yourself, E.g.:
short a;
long b;
char buf[6];
read(fd, buf, sizeof(6));
a = (buf[0] << 8) | buf[1];
b = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | buf[5];
This is most significant byte first. You may choose differently. If you
are used to the Intel world, you might pick least significant byte first.
a = buf[0] | (buf[1] << 8);
Ugly, but it works on every platform I've tried.
--
============================================
Without my guitar, I am a poet without arms.
- Michael Bloomfield
============================================
- Raw text -