c++ normalizing data sizes across systems

Posted by Bocochoco on Stack Overflow See other posts from Stack Overflow or by Bocochoco
Published on 2010-12-28T18:46:37Z Indexed on 2010/12/28 18:54 UTC
Read the original article Hit count: 181

Filed under:
|

I have a struct with three variables: two unsigned ints and an unsigned char. From my understanding, a c++ char is always 1 byte regardless of what operating system it is on. The same can't be said for other datatypes. I am looking for a way to normalize POD's so that when saved into a binary file, the resulting file is readable on any operating system that the code is compiled for.

I changed my struct to use a 1-byte alignment by adding #pragma as follows:

#pragma pack(push, 1) 
struct test
{
   int a;
}
#pragma pack(pop)

but that doesn't necessarily mean that int a is exactly 4 bytes on every os, I don't think? Is there a way to ensure that a file saved from my code will always be readable?

© Stack Overflow or respective owner

Related posts about c++

Related posts about binaryfiles