将CString输出到std::cout

9
如何将CString打印到控制台?尝试使用以下代码,但输出结果似乎是指针。
..
#include <iostream>
#include <atlstr.h>

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

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

Output 00F56F0
3个回答

6
使用以下内容:
std::wcout << a.GetString();

4

使用wcout将CString打印到控制台:

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

2
如何将CString打印到控制台?尝试了这段代码,但得到的是指针打印出来的结果。
抱歉,我还没有完成就被打断了。显然,你需要转换为临时的CStringA(否则它是宽字符串格式,即wcout)。直到我再次阅读你的消息,我才意识到这一点。
std::ostream& operator << ( std::ostream& os, const CString& str )
{
  if( str.GetLength() > 0 )  //GetLength???
  {
    os << CStringA( str ).GetString();
  }
  return os;
}

你当然可以像建议的那样使用wcout:

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

然后像这样使用:
std::wcout << str << std::endl;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接