B
Bill Woessner
Suppose I have a structure, foo, which is a POD. I would like to read
and write it to disk as follows:
std:fstream outs;
foo bar;
outs.write(reinterpret_cast<char*>(&bar), sizeof(foo));
....
std::ifstream ins;
foo bar;
ins.read(reinterpret_cas<char*>(&bar), sizeof(foo));
This works fine if the code doing the reading and writing are compiled
with the same compiler. However, I now have a situation where I would
need to write with one compiler and read with another. Fortunately,
the reading and writing will occur on the same platform, so endianness
is not a problem.
Is there a guaranteed way to accomplish this? I'm guessing the reason
it's not working has to do with padding and byte-alignment. Is there
some way to force the two compilers to agree on padding and byte-
alignment?
Thanks in advance,
Bill
and write it to disk as follows:
std:fstream outs;
foo bar;
outs.write(reinterpret_cast<char*>(&bar), sizeof(foo));
....
std::ifstream ins;
foo bar;
ins.read(reinterpret_cas<char*>(&bar), sizeof(foo));
This works fine if the code doing the reading and writing are compiled
with the same compiler. However, I now have a situation where I would
need to write with one compiler and read with another. Fortunately,
the reading and writing will occur on the same platform, so endianness
is not a problem.
Is there a guaranteed way to accomplish this? I'm guessing the reason
it's not working has to do with padding and byte-alignment. Is there
some way to force the two compilers to agree on padding and byte-
alignment?
Thanks in advance,
Bill