[Ilugc] c/c++ endian neutral disk i/o
girishvenkatachalam at gmail.com
Thu Oct 7 08:35:05 IST 2010
On Thu, Oct 7, 2010 at 3:02 AM, narendra babu <cnarendra_babu at yahoo.com> wrote:
> Hello all ,
> I have 2 questions on endian neutral :
> 1)I'm dont need C++ Object Serialization per-se but I intend to write a few C structures to disk (file.bin) -
> The written data structure on-disk should be portable across different platform architectures (small/big endian)
If you use a character buffer C struct then it is endian neutral. Then
when you read back the structure by doing a cast using
struct mydata *nw_struct;
nw_struct = malloc(sizeof(mydata);
memcpy((struct mydata *) &nw_struct,
buf, sizeof(struct mydata));
Now you can send and receive from
across the network in an Endian neutral fashion.
If you have longs or shorts inside
the struct, just use htonl(), htons() and vice versa.
This will surely work.
> Would someone know where I can find some library and/or sample code to write portable C data structure to a disk storage.
> at least some guidelines and pointers / examples would be nice.
You have the code to achieve it.
> 2) if have solaris code which genrates binary file and i have read it from Linux program .
On Sparc architecture? No problem. It will work. I have done that.
> so any specific thing i need to take care so that i can read big endian generated from soalris from Linux program which is on little endian
Nothing more than what I wrote above.
Endianness does not hurt when you use unsigned char buffers.
girish at gayatri-hitech.com
More information about the ilugc