I have a variable of type LPTSTR
, which I print to std::cout
with <<
. In an ANSI system (don't know exactly where it is determined) it worked fine, it printed the string. Now in a Unicode system I get a hex address instead of the string. So, why does LPSTR
(to which LPTSTR
is resolved if UNICODE
is not defined) act differently from LPWSTR
(... if UNICODE
is defined) and how do I print the string pointed by the latter one?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
For Unicode strings you want wcout
.
You may be seeing hex because the ANSI/ASCII output stream doesn't know how to handle Unicode characters.
LPTSTR
and LPWSTR
are actually C-isms inherited from the C Windows API days. For C++ I would strongly encourage you to use std::string
and/or std::wstring
instead.
If you need to roll your own macro, you'll want something like:
#ifdef _UNICODE
#define COUT wcout
#else
#define COUT cout
#endif