Re: sizeof (struct) differs from Windows and Linux. How can I fix this?
- From: John Cupitt <jcupitt gmail com>
- To: edward hage <edha xs4all nl>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: sizeof (struct) differs from Windows and Linux. How can I fix this?
- Date: Wed, 29 Dec 2004 18:06:59 +0000
Hi Edward,
On Wed, 29 Dec 2004 18:57:41 +0100, edward hage <edha xs4all nl> wrote:
Now on Linux the size of the structs are resp. : 388 and 172.
And on Windows XP (cross-compile mingw on Linux): 392 and 176.
This is part of C: you can't write() a struct on one machine and
read() it on another. They may have different alignment rules (I think
this is the cause of your problem), or even different endian-ness.
The portable way to read a struct from a file is to write a reader
function for every type you want to read from the file, and then to
call them in order. Something like:
int
read_uint16( int fd, guint16 *out )
{
guint8 t1, t2;
if( read( fd, &t1, 1 ) != 1 || read( fd, &t2, 1 ) != 1 )
return( -1 );
// or the other way around, depending on file endianness
*out = t1 << 8 | t2;
return( 0 );
}
int
read_foo_from_file( int fd, struct Foo *foo )
{
if( read_uint16( fd, &foo->a ) || read_uint16( fd, &foo->b ) )
return( -1 );
return( 0 );
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]