Re: Structure size and binary format



On 30 Dec 2005 16:05:03 -0800, in comp.lang.c , "gamehack"
<gamehack@xxxxxxxxx> wrote:

>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?

The simplest is to store the data as text, not binary data. Other
methods might involve using fixed-width data types (if your platforms
support them), or writing custom load/save functions for each platform
which still store in binary but do it element by element and take into
account the differing sizes of types on each platform.


Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
.



Relevant Pages

  • [patch 1/1] Hot plug CPU to support physical add of new processors (i386)
    ... The patch allows physical bring-up of new processors from facilities such as driver/utility implemented on a platform. ... +static unsigned int __devinitdata num_processors; ...
    (Linux-Kernel)
  • Re: Question about bit-fields and portability
    ... unsigned int val1: 1; ... How can such a bit-field struct be used in a non portable way? ... network) and then try to restore it on a platform with different ... or that a plain int bitfield of 1 bit can have two ...
    (comp.lang.c)
  • Re: Accessing members of array as a different type
    ... > basic question. ... > platform). ... How do I access it as an unsigned int? ... error because of an unaligned access. ...
    (comp.lang.c)
  • Re: Simple question about pointer types
    ... gazelle@xxxxxxxxxxxxxxxxxxx (Kenny McCormack) wrote: ... >>> concerned.The size of unsigned int varies from platform to platform, ... > to include an obvious qualification, which I'm sure to him seemed so ...
    (comp.lang.c)
  • Re: Writing a structure
    ... Use a binary file and dump the binary representation of the ... > compiler, on the same platform. ... shared binary data files between 68k and SPARC. ...
    (comp.lang.c)