2014-09-12 65 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;