Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CString to std::cout

Tags:

c++

visual-c++

How to print CString to console? Trying this code, but got something like pointer is printed.

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

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

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

Output 00F56F0
like image 279
vico Avatar asked Sep 12 '14 15:09

vico


3 Answers

Use following :

std::wcout << a.GetString();
like image 115
P0W Avatar answered Oct 19 '22 10:10

P0W


Use wcout to print CString to console:

CString cs("Hello");
wcout << (const wchar_t*) cs << endl;
like image 30
Deadlock Avatar answered Oct 19 '22 09:10

Deadlock


How to print CString to console? Trying this code, but got something like pointer is printed.

My apologies. I was not finished and got interrupted. Apparently you have to convert to a temporary CStringA (otherwise it is wide string format i.e wcout). I did not realise this until I read your message (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 of course 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;
like image 34
Werner Erasmus Avatar answered Oct 19 '22 09:10

Werner Erasmus