Reputation: 167
I've been scratching my head on a very odd issue. See sample code below:
test_c.c
#include <wchar.h>
#include <stringapiset.h>
int main (int argc, char **argv)
{
const char mystr[] = "The brown fox jumped over the lazy dog";
wchar_t wideName[MAX_PATH];
int len = MultiByteToWideChar(CP_UTF8, 0, mystr, -1, wideName, MAX_PATH-1);
wprintf( L"Length = %d - String = %s\n", len, wideName );
return 0;
}
Compilation command quite straightforward:
gcc -static test_c.c -o test_c.exe
The first odd thing that's happening, is that the result I get is different depending on the toolchain I'm using:
Length = 39 - String = The brown fox jumped over the lazy dog
Length = 39 - String = T
To add to this strangeness, if I make it a C++ file now:
test_cpp.cpp
//#include <stdio.h>
#include <cwchar>
#include <stringapiset.h>
int main (int argc, char **argv)
{
const char mystr[] = "The brown fox jumped over the lazy dog";
wchar_t wideName[MAX_PATH];
int len = MultiByteToWideChar(CP_UTF8, 0, mystr, -1, wideName, MAX_PATH-1);
wprintf( L"Length = %d - String = %s\n", len, wideName );
return 0;
}
and if I use the same compilation command:
g++ -static test_cpp.cpp -o test_cpp.exe
then I get a something even more strange:
Length = 39 - String = T
Am I doing something wrong ? Is there a way to fix this ?
Thanks !
Upvotes: 0
Views: 264
Reputation: 140990
Am I doing something wrong ? Is there a way to fix this ?
Yes. %s
is for printing char*
. To print wchar_t*
, use %ls
.
Upvotes: 1