C ++, WCHAR [] in std :: cout and comparison

I need to put WCHAR [] in std :: cout ... This is the part of PWLAN_CONNECTION_NOTIFICATION_DATA passed from the native WiFi API callback.

I tried just std :: cout <<var; but it prints the numeric address of the first char. comparison ( var == L"some text" ) does not work either. The debugger returns the expected value, however, the comparison returns 0. How to convert this array to a standard string (std :: string)?

Thank you in advance

+1
c ++ winapi wchar-t widechar
Oct 26 '09 at 15:39
source share
4 answers

Assuming var is wchar_t * , var == L"some text" does a pointer comparison. To compare the string that var points to, use a function like wcscmp .

+3
Oct 26 '09 at 15:50
source share

Some solutions:

  • Write to std :: wcout instead
  • Convert:
    • Standard way using std :: codecvt
    • Win32 path using WideCharToMultibyte
+12
Oct 26 '09 at 15:44
source share

To print to cout , use std::wcout .

As for the comparison, I'm not quite sure what you mean.

  • if var is wchar_t[] , then you are comparing two pointers. And the result is likely to be false, because although the contents of the string may be the same, they are physically allocated in different memory cells. The answer is to use a function like strcmp that compares C-style strings (char pointers) or uses a C ++ string class.
  • and operator== usually returns bool , not an integer. Therefore, it can return false , but it cannot return 0 ... If you did not create some strange overload yourself. (and this is only possible if var is a user-defined type.
+8
Oct 26 '09 at 15:46
source share

use the following

 #ifdef UNICODE #define tcout wcout #else #define tcout cout #endif 
+3
Jun 01 '10 at 17:21
source share



All Articles