C++ 在控制台上打印 TCHAR[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2812760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print TCHAR[] on console
提问by hara
I'm quite sure that it is a stupid issue but it drives me crazy..
我很确定这是一个愚蠢的问题,但它让我发疯..
how could i print on the console a TCHAR array?
我怎么能在控制台上打印一个 TCHAR 数组?
DWORD error = WSAGetLastError();
TCHAR errmsg[512];
int ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, 0, errmsg, 511, NULL);
i need to print errmsg...
我需要打印 errmsg ...
回答by Thomas
It depends on what TCHAR
is. If you compile with Unicode enabled, TCHAR
is defined as wchar_t
. Then you can use std::wcout
, for example:
这取决于是什么TCHAR
。如果在启用 Unicode 的情况下编译,TCHAR
则定义为wchar_t
. 然后你可以使用std::wcout
,例如:
std::wcout << L"Error: " << errmsg << '\n';
If Unicode is not enabled, TCHAR
is an ordinary char
and you can use the ordinary std::cout
:
如果未启用 Unicode,TCHAR
则是普通的char
,您可以使用普通的std::cout
:
std::cout << "Error: " << errmsg << '\n';
回答by Jacob
A Google search revealed this discussionwhich essentially recommends tprintf.
回答by Lumi
#include <tchar.h> /* _tprintf */
DWORD dwError;
BOOL fOk;
HLOCAL hlocal = NULL; // Buffer that gets the error message string
fOk = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError, 0, (PTSTR) &hlocal, 0, NULL);
if (! fOk) hlocal = TEXT("Fehler FormatMessage");
_tprintf( TEXT("%d\t%s\n"), dwError, hlocal );
if (fOk) LocalFree(hlocal);
回答by José Mendes
I really don't know why but this code worked for me:
我真的不知道为什么,但这段代码对我有用:
TCHAR NPath[MAX_PATH];
DWORD a = GetCurrentDirectory(MAX_PATH, NPath);
string b = "";
for(int i=0; i<a;i++){
b+=NPath[i];
}
cout << b;
system("pause");
Sorry but i can't really explain why it works and don't have time to search it now. Later!
抱歉,我无法真正解释它为什么有效,现在也没有时间搜索它。之后!