我有幾個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