C++, WCHAR[] to std::cout and comparision
Question
I need to put WCHAR[] to std::cout ... It is a part of PWLAN_CONNECTION_NOTIFICATION_DATA passed from Native Wifi API callback.
I tried simply std::cout << var; but it prints out the numeric address of first char. the comparision (var == L"some text"
) doesn't work either. The debugger returns the expected value, however the comparision returns 0. How can I convert this array to a standard string(std::string)?
Thanks in advance
Solution
Assuming var is a wchar_t *
, var == L"some text"
does a pointer comparison. In order to compare the string pointed to by var, use a function such as wcscmp
.
OTHER TIPS
Some solutions:
- Write to std::wcout instead
- Convert:
- The standard way, using std::codecvt
- The Win32 way, using WideCharToMultibyte
For printing to cout
, you should use std::wcout
instead.
As for the comparison, I'm not really sure what you mean.
- if
var
is awchar_t[]
, then you are comparing two pointers. And the result will most likely be false, because while the string contents may be the same, they are physically allocated in different memory locations. The answer is to either use a function likestrcmp
which compares C-style strings (char pointers), or to use the C++ string class. - and the
operator==
usually returns abool
, not an integer. So it can returnfalse
, but it can't return0
... Unless you've created some weird overload yourself. (and that is only possible ifvar
is a user-defined type.
use the following
#ifdef UNICODE
#define tcout wcout
#else
#define tcout cout
#endif