【发布时间】:2011-07-29 21:08:02
【问题描述】:
我有一些 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 那么 strary 应该是 wchar_t 而不是 char。
标签: c++