Neil Benn
Neil Benn

Reputation: 926

convert wchar_t into wstring

Sorry to ask such a simple question but I am trying to put a wchar_T into a wstring but when I print it out to check it I get garbage from the wstring. The code is below:

 wchar_t windowsDir[255];
 memset(windowsDir, NULL, sizeof(windowsDir));
 GetWindowsDirectory(windowsDir, 255);
 std::wstring winDir(windowsDir);

 if (debug){
   wprintf(L"Windows Dir is %s\r\n", windowsDir);
   wprintf(L"Windows Dir is %s\r\n", winDir);
 }

As far as I can tell from the examples I'm doing the right thing so I cannot work out shy I'm getting garbage on the screen.

Thanks, in advance, for your help,

Cheers,

Neil

Upvotes: 0

Views: 828

Answers (1)

Puppy
Puppy

Reputation: 146910

You're using wprintf. This function cannot accept a std::wstring. The primary reason that you should never, ever, use printf and friends or any variable arguments function is that they are completely un-type-safe, and cannot cope with non-POD types in addition- including any C++ Standard library type.

A C++ IOstream would function perfectly correctly here.

Upvotes: 1

Related Questions