I have a few CString variables that I wish to output to an ofstream, but I can’t seem to figure out how to do so. Here’s some example code.
I have unicode enabled in VS 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!
Any ideas? Thanks!
If you just want plain ACP encoded ANSI text:
ofstream formatted output functions expect narrow/ansi strings.
CStrings represent TCHAR strings.
The CT2A macro Converts TCHAR 2 Ansi.
http://msdn.microsoft.com/en-us/libr…8VS.80%29.aspx