Structure size and binary format



Hi all,

I've been wondering when I write a structure like:

struct {
int a;
unsigned int b;
float c;
} mystruct;

And then I'm using this as a record for a binary file. The problem is
that the size of the types is different on different
platforms(win/lin/osx) so if a file was copied on another platform and
attempted to be read then the first say 16 bytes could be regarded as
the integer a but it could have been created on system where integer
was 32 bytes. Is there a portable solution to this? Moreover, I've been
looking for some resource on designing your own binary format and I
couldn't find anything apart from short tutorials how to read binary
files. Are there any good resources?

Thanks a lot

.



Relevant Pages

  • Re: Intent of C99 6.5#6 ?
    ... In snippet #2, the effective type of y is always 'unsigned int' ... There are a significant number of cases of aliasing that the standard ... union uf {float uff; unsigned long uul;}; ...
    (comp.std.c)
  • Re: Using underlying bit representations of floats
    ... size/alignment as float or if unsigned int has any trap representations ... creating test cases with intentional violations. ... representations of floats is a straight violations of MISRA C 2004 rule ...
    (comp.lang.c)
  • Re: Float comparison
    ... <more snip> ... You seem to believe that the float therefore represents all those ... an unsigned int also represents an infinite ... Similarly floats are not reals, ...
    (comp.lang.c)
  • Re: Convert float to double - weird failure
    ... bill robertson wrote: ... Float 1.2 is uninterpreted hex 0x3f99999a ... It's converting the float representation of 1.2 which is 0x3f99999a to a double representation which is not even 0x3f99999a00000000. ... unsigned int mantissa:23; ...
    (comp.programming)
  • [RFC][PATCH 2/9] CPU controller - Adds class hungry detection
    ... adding the detection code that checks whether a task group needs more CPU ... The CPU resource controller have to distinguish whether ... unsigned int slice, ...
    (Linux-Kernel)