I have a simple vector of ints, and I want to write it into a binary file. For example:
#include <fstream>
#include <vector>
int main () {
std::vector<uint32_t> myVector{5, 10, 15, 20 };
// write vector to bin file
std::ofstream outfile("./binary_ints.data", std::ios_base::binary|std::ios::trunc);
std::copy(myVector.begin(), myVector.end(), std::ostreambuf_iterator<char>(outfile));
outfile.close();
return 0;
}
Then, if I inspect the file "binary_ints.data" in hex mode, I have this:
00000000: 050a 0f14 0a
Thats ok!.
However, If myVector has this data:
std::vector<uint32_t> myVector{3231748228};
Then, the hex stored is weird:
00000000: 840a
84 in Hex doesn't match with the Int 3231748228.
What's happening here?
Thanks.
std::copydoesn't transform one element into multiple new elements, nor doesstd::ostreambuf_iterator. - chrisintvalue is > 255. - PaulMcKenzie