If I write an int into a fstream in a 32 application and the read that int back in a 64 bit application, should I expect the value to be different? If so (and I presume it is), what is the best way to achieve architecture-independent stream operations?
If you read and write using operator<< and operator>>, it will be platform independent, assuming that the integer is small enough to fit in both types, since if will be written as text. If you use ostream::read and osteam::write, it will not be platform independent, since you will be writing binary data.
If you don't need raw performance, using a text format is the easiest way to achieve platform independence. If you need better performance, you should look at a serialization library. Boost has a good cross-platform one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With