I recommend using ofstream (not wofstream) and writing it in binary. How exactly wofstream reads/writes characters is not always that good. In VC++ that string is converted back to a multibyte code page when it's written. E.g. writing a wstring (UTF-16) with japanese characters on my system will result in SJIS encoded text.
I'd recommend this method to write:
Code:
std::ofstream outFile("filename.dat", std::ios::out | std::ios::binary);
outfile.write((char *) wstr.c_str(), wstr.length() * sizeof(wchar_t));
Oh, also keep in mind, if you're trying to write a text file for an editor like notepad, it expects the first character in the file to be a unicode byte-order marker (0xFEFF). So to write that:
Code:
wchar_t BOM = 0xFEFF;
std::ofstream outFile("filename.dat", std::ios::out | std::ios::binary);
outfile.write((char *) &BOM,sizeof(wchar_t));