Attila Feher said:
Thomas Matthews wrote:
[SNIP]
Try this:
ofstream data_file("my_data.bin", ios_base::binary);
Record data;
data_file.write((unsigned char *) &data, sizeof Record);
The above technique assumes that the char arrays are fixed
size and it writes it in unformatted, binary mode.
And will possibly not be able to read it back on another architecture.
....or the same architecture with another compiler, or the same
architecture and compiler with different compiler options.
The FAQ has a little bit about this, although it's less about
portability aspects and more about how to output whole data
structures:
<
http://www.parashift.com/c++-faq-lite/serialization.html>
If you care at all about portability, you might first consider a
purely textual format (XML seems popular these days), possibly
compressed (e.g., via gzip). If you find that too bulky and/or slow
for your purposes, standardize on a binary format (e.g., 32-bit
little-endian integer, 64-bit IEEE floating point number, strings with
leading length indicator, etc.) and figure out how to portably read
and write in that format -- taking into account all supported
combinations of architecture, compiler, and compiler options, of
course.
- Shane