C++_TCHAR字串在UNICODE環境的控制檯下輸出
阿新 • • 發佈:2018-12-27
TCHAR字串在UNICODE環境下
是不能直接用cout 輸出的, 原因是 cout 為 ostream流物件例項,直接用cout 輸出TCHAR字串會輸出字串的地址
</pre><pre name="code" class="cpp"> //TODO:測試程式碼 //得到了非空一個字串,則查詢成功。 //...... //.....將該字串轉換為TCHAR..... //...... USES_CONVERSION; const TCHAR *tempID_Result; //返回得到的ID tempID_Result = A2CT(ID_ResultTemp); for (int i = 0; i < m_table.size(); i++) { if (0 == _tcscmp(tempID_Result, m_table[i].ID)) { wcout << "你好:" << &(m_table[i].UserName) << " 通過了認證" << endl; cout << "你好:" << (m_table[i].UserName) << " 通過了認證" << endl; wcout << "你好:" << (m_table[i].UserName) << " 通過了認證" << endl; //_tprintf(_T("XXXXXXXXXXXXXXXXX\n")); //_tprintf(_T("你好: 通過了認證\n")); //_tprintf(_T("你好: %s 通過了認證\n"), m_table[i].UserName); //_tprintf(_T("XXXXXXXXXXXXXXXXX\n")); } } //END: 測試程式碼
截圖
可以看出來 cout 在Unicode環境下 只能輸出地址 ,
所以不應該使用cout, 可以使用wcout。
wcout過載了TCHAR,可以對TCHAR進行輸出,因為TCHAR在Unicode中即定義為WCHAR。
想了一個辦法,先對環境進行判斷然後呼叫適當的輸出函式
示例程式碼如下:
//TODO:測試程式碼 const TCHAR *tempID_Result; //返回得到的ID tempID_Result = A2CT(ID_ResultTemp); for (int i = 0; i < m_table.size(); i++) { if (0 == _tcscmp(tempID_Result, m_table[i].ID)) { #ifdef _UNICODE wcout << "你好:" << (m_table[i].UserName) << " 通過了指靜脈認證" << endl; #endif #ifdef _MBCS cout << "你好:" << (m_table[i].UserName) << " 通過了指靜脈認證" << endl; #endif } } //END: 測試程式碼
此外注意:在UNICODE環境下TCHAR字串如果是中文, 是不能用_tprintf輸出的。
示例
#include <iostream>
#include <tchar.h>
using namespace std;
int main()
{
_tprintf(_T("你好啊\n"));
return 0;
}
顯示不出來。。。。。。