Currently, on 32bit machine I write binary data to the file like this:
outbin.write( reinterpret_cast <const char *> ( &key_session ), sizeof( unsigned int ) );
outbin.write( reinterpret_cast <const char *> ( &last_access_time ), sizeof( time_t ) );
outbin.write( reinterpret_cast <const char *> ( &sizet ), sizeof( size_t ) );
outbin.write( reinterpret_cast <const char *> ( &ddd), sizeof( double ) );
outbin.write( reinterpret_cast <const char *> ( &fff), sizeof( float ) );
How to write data to make it portable and be sure that data will be loaded at any machine (64bit) ? (maybe when operation system will be changed at the machin to 64bit or data wil be copied to other machine)
In C++0x, use types like
std::uint32_tfrom<cstdint>. In older compilers, use the C99 header<stdint.h>; this is missing from MSVC, but various implementations of it float around the web. You’ll have to cast all yourintandsize_tvariables to<cstdint>types when writing and back to native types when reading.floatanddoubledo not have portable, known-width versions, so you might have to assume that they match single and double formats from IEEE 754. Some assertions might help here.As for
time_t, note that it has no standardized meaning across platforms except that it can represent time in some unspecified way.(Also, keep in mind that you might want to write data out in an endian-neutral format.)