我有几个 CString 变量希望输出到 ofstream,但我似乎不知道如何输出。这是一些示例代码。
我在 VS 2005 中启用了 unicode
代码:
ofstream ofile;
CString str = "some text";
ofile.open(filepath);
ofile << str; // doesn't work, gives me some hex value such as 00C8F1D8 instead
ofile << str.GetBuffer(str.GetLength()) << endl; // same as above
ofile << (LPCTSTR)str << endl; // same as above
// try copying CString to char array
char strary[64];
wcscpy_s(strary, str.GetBuffer()); // strary contents are correctly copied
ofile << strary << endl ; // same hex value problem again!
有什么想法吗?谢谢!
最佳答案
如果使用 UNICODE,为什么不使用 wofstream,它是一个用 wchar_t 参数化的 basic_stream。这按预期工作:
CString str = _T("some text");
std::wofstream file;
file.open(_T("demo.txt"));
file << (LPCTSTR)str;
关于c++ - 如何将 CString 转换为 ofstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5550838/