CString to std :: cout

How to print CString to console? Tried this code but got something like a pointer.

..
#include <iostream>
#include <atlstr.h>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    CString a= "ddd";
    cout<<a.GetString();
}

Output 00F56F0

      

+3


source to share


3 answers


Use the following:



std::wcout << a.GetString();

      

+5


source


Use wcout to print CString to console:



CString cs("Hello");
wcout << (const wchar_t*) cs << endl;

      

+2


source


How to print CString to console? Tried this code but got something as a pointer.

My apologies. I didn't finish and was interrupted. Obviously you need to convert to temporary CStringA (otherwise it is wide format ie wcout). I didn't get it until I read your post (again):

std::ostream& operator << ( std::ostream& os, const CString& str )
{
  if( str.GetLength() > 0 )  //GetLength???
  {
    os << CStringA( str ).GetString();
  }
  return os;
}

      

You could, as suggested, just use wcout:

std::ostream& operator << ( std::wostream& os, const CString& str )
{
  if( str.GetLength() > 0 )  //GetLength???
  {
    os << CStringA( str ).GetString();
  }
  return os;
}

      

Then use like this:

std::wcout << str << std::endl;

      

+1


source







All Articles