2014-09-12 64 views
3

如何将CString打印到控制台?尝试这个代码,但得到像指针这样的东西被打印出来。CString to std :: cout

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

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

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

Output 00F56F0 

回答

1

如何打印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; 
5

使用以下内容:

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

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

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