I'm trying to use std::wofstream/wifstream to serialize/deserialized mixed numerical (e.g., int, double) and std::wstring content, with the numerical values serialized in binary mode. But the following code produces erroneous results:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
wofstream wos("Test.dat", std::ios_base::binary);
int x1 = 100;
double d1 = 50.0;
wstring ostr(L"my string");
wos.write(reinterpret_cast<wchar_t*>(&x1), sizeof(x1) / sizeof(wchar_t));
wos.write(reinterpret_cast<wchar_t*>(&d1), sizeof(d1) / sizeof(wchar_t) );
wos << ostr;
wos.close();
int x2 = 0;
double d2 = .0;
wstring istr;
wifstream wis("Test.dat", std::ios_base::binary);
wis.read(reinterpret_cast<wchar_t*>(&x2), sizeof x2 / sizeof(wchar_t));
wis.read(reinterpret_cast<wchar_t*>(&d2), sizeof d2 / sizeof(wchar_t));
wis >> istr;
wis.close();
wcout << x2 << "\n" << d2 << "\n" << istr << "\nistr.size() = " << istr.size() << endl;
return 0;
}
**Expected output:**
100
50
my string
istr.size() = 9
**Actual output:**
100
0
istr.size() = 0
So, the double value and wstring are corrupted. The code works properly when serializing only the wstring (using <<) and not the binary numericals, or when using ofstream, ifstream and string rather than their wide-character equivalents. I suspect the problem lies in writing and reading in the int and double in two-byte chunks, but I'm at loss as to why. Advice appreciated.
--- EDIT ---
Thanks to everyone for the answers. I'd been using Qt's QDataStream to write these kinds of files without issue, but that abstracted away the problems that surfaced above. Takeaway is to stick with ifstream/ostream or, better yet, use a library (whether Qt's or the others suggested).
wos.writeattemps to convert eachwchar_telement to the system default codepage. It just so happens that byte pairs in the byte representation ofx1are in the ASCII range, but bytes ind1representation are not. If you examine the file, it's like 5 bytes large - the secondwritecall fails midway,wosis marked withbadbit, and the string output is not even attempted.writeultimately callsstd::basic_filebuf<CharT,Traits>::overflowon each character, and that one is documented to "... usestd::codecvt::outof the imbued locale to convert the characters into external (possibly multibyte) representation...". Opening the file in binary mode doesn't do what you think it does (roughly, text vs binary mode makes no difference on Linux-y systems; on Windows, it suppesses conversion of\ncharacter to CR LF pair).wofstreamis simply not suitable for writing binary data.