wprintf does not work correctly.

Started by edison, June 08, 2014, 08:31:43 PM

Previous topic - Next topic

edison

SetConsoleOutputCP(65001) does not work, but 936 works.

#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main(void)
{
wchar_t *a = L"中文";
DWORD n;
SetFileApisToOEM();
SetConsoleOutputCP(936);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), a, wcslen(a), &n, NULL);

wprintf(L"%ls\n", a);
wprintf(L"%ls\n", L"中文");
return 0;
}


output:
Win 8.1 x64 chs:
中文-?
-?
Press any key to continue...


the wprintf still not work in right way.

frankie

Something like this works?

char MbStr[32];
WideCharToMultiByte(936, WC_DEFAULTCHAR, L"中文", -1, MbStr, 32, NULL, NULL);
printf("%s\n",MbStr);
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

edison

#17
#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main(void)
{
char MbStr[32];
WideCharToMultiByte(936, WC_DEFAULTCHAR, L"中文", -1, MbStr, 32, NULL, NULL);
printf("%s\n", MbStr);
return 0;
}


the output(wrong):
)
Press any key to continue...


jj2007

Quote from: czerny on June 12, 2014, 01:09:12 PM
Have you created this as a windows project or a console project?
Quote from: frankie on June 12, 2014, 01:24:43 PM
Add '-subsystem:windows' on linker command line or select subsystem=windows form linker tab.

Thanks. Adding user32.lib, enabling M$ extensions and changing to sub windows made it finally work. Would be great if the IDE was able to guess these settings automatically, or at least throw warnings of the "wWinMain detected - are you sure it's a console app?" style...

frankie

Quote from: edison on June 12, 2014, 07:52:17 PM
#include <stdio.h>
#include <wchar.h>
#include <windows.h>

int main(void)
{
char MbStr[32];
WideCharToMultiByte(936, WC_DEFAULTCHAR, L"中文", -1, MbStr, 32, NULL, NULL);
printf("%s\n", MbStr);
return 0;
}


the output(wrong):
)
Press any key to continue...

Ya it seems that the low level part of a file writing on printf family is unaware of encodings and codepages. This probably lead to mishandling of strings.  :-X
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

edison

The wprintf of POCC 8.0 RC5 still not work correctly, nothing changed here.

frankie

Yes, Pelle want the IO subsystem to comply with Cxx standard, while MS subsystem is not compliant...
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

edison

Still not works in 8.0 RC6.
Is there any chance to fix this problem in furture version ?