我有几个CString变量,我想输出到一个ofstream,但我似乎无法弄清楚如何做到这一点。这是一些示例代码。如何将CString转换为ofstream?
我在VS支持Unicode 2005
CODE:
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。 – 2011-04-05 11:21:56