Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: 32 vs 64 bit stream operations

Tags:

c++

stream

64-bit

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?

like image 618
krojew Avatar asked Dec 07 '25 19:12

krojew


1 Answers

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.

like image 182
Dirk Holsopple Avatar answered Dec 09 '25 15:12

Dirk Holsopple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!