C++: LPWSTR prints as an address in cout

青春壹個敷衍的年華 提交于 2019-11-28 08:02:52

问题


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
std::wostream& COUT = std::wcout;
#else
std::ostream& COUT = std::cout;
#endif


来源:https://stackoverflow.com/questions/5873384/c-lpwstr-prints-as-an-address-in-cout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!