我想TCHAR在 Visual Studio 中使用 , 并將字符集設定為 UNICODE,也許現在我可以得到寬字符的結果,即 16 位 Unicode 系統,但它不起作用。
這是我的代碼:
#include<Windows.h> //to use windows API
#include<iostream>
int main()
{
TCHAR a[] = TEXT("This is not ANSI anymore! Olé!"); //8bits each char
wchar_t b[] = L"This is the Unicode Olé!"; //16 bits each char
std::cout << a << "\n";
std::wcout << b << "\n";
return 0;
}
所以我想,在定義之后TCHAR,我可以利用:
#ifdef UNICODE
#define std::cout std::wcout
#else
#define std::cout std::cout
#endif
但是,我的輸出仍然是十六進制的TCHAR a[],但是為什么呢?它應該wcout自動使用,對吧?
uj5u.com熱心網友回復:
std::cout不支持wchar_t字串,std::wcout也不支持char字串。因此,您必須根據TCHAR使用的字符型別來選擇其中一個。
您嘗試使用#define來解決這個問題是正確的,但是您使用了錯誤的語法。
試試這個:
#include <Windows.h> //to use windows API
#include <iostream>
#ifdef UNICODE
#define t_cout std::wcout
#else
#define t_cout std::cout
#endif
int main()
{
TCHAR a[] = TEXT("Olé!");
t_cout << a << TEXT("\n");
// or: t_cout << a << std::endl;
return 0;
}
uj5u.com熱心網友回復:
在帶有 set 的 Windows 中UNICODE,TCHAR 輸出為wchar_t.
您不能使用std::cout寬字符。它只使用char. windows.h 并沒有按照您的想法重新定義 cout。
因此,就好像您正在將一個(有符號或無符號)陣列輸出short到流中。陣列衰減為指標,這可能就是您看到十六進制的原因。
然而
std::wcout << a << "\n";
應該管用。
uj5u.com熱心網友回復:
宏不正確,您還需要使用_setmode正確的控制臺輸出:
#include <Windows.h> //to use windows API
#include <iostream>
#include <fcntl.h>
#include <io.h>
#ifdef UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif
int main()
{
_setmode(_fileno(stdout), _O_WTEXT);
TCHAR a[] = TEXT("This is not ANSI anymore! Olé!");
tcout << a << TEXT("\n");
}
uj5u.com熱心網友回復:
只有 windows API 中存在的函式才有可能,因為您的 windows.h 對每個 ANSI 和 unicode 形式都有兩個函式,而 tchar 只會對那些存在于那里的函式進行更改。
cout 和 wcout 在 Windows API 中不存在它在 iostream 中存在所以沒有變化
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477230.html
