1

If I use the fputws without setlocale, only ASCII letters get printed. It seems that setlocale is necessary, and according to this site, both setlocale(LC_CTYPE, "UTF-8") and setlocale(LC_CTYPE, "en_us.UTF-8") would work.

But when I tried it, setlocale(LC_CTYPE, "UTF-8") did not work, and it printed "Hello ". Only with setlocale(LC_CTYPE, "en_us.UTF-8"), I get "Hello ねこ 2". I read the comment on that page, and he also said "UTF-8" did not work (he used GCC, I used VC++). I read some manual pages, but they did not specifically say what the format should be. On CPP reference, the example is using only formats like "en_US.UTF-8", but on tutorials point, the example uses formats like "en_GB".

Is setlocale(LC_CTYPE, "UTF-8") a correct call?

setlocale(LC_CTYPE, "UTF-8");
//setlocale(LC_CTYPE, "en_us.UTF-8");
FILE* output = _wfopen(L"output.txt", L"w");
fputws(L"Hello ねこ 2", output);
fclose(output);
Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

0

The answer is system specific. To quote the cppreference page you linked to:

system-specific locale identifier. Can be "" for the user-preferred locale or "C" for the minimal locale

I think "UTF-8" is an invalid locale name generally (it needs the language identifier at minimum, not 100% on that though). However generally you will want to simply use "" in order to default to the user preference (which, I would hope, is correct for displaying their input).

Object object
  • 1,939
  • 10
  • 19
  • This lack of consistency is kind of annoying.`setlocale(LC_CTYPE, "enUS-UTF-8")` worked on Windows 10, but when I ran the same code on Windows Server 2019, it did not work (Japanese was not printed). I changed LC_CTYPE to LC_ALL, then it worked. I guess trial and error is the only way... – Damn Vegetables Sep 05 '20 at 14:15
  • @DamnVegetables In older Windows you have to use UTF-16 because UTF-8 isn't allowed as a locale. But you can link with the new stdlib from MS to use UTF-8 as a locale in older Windows. [What is the Windows equivalent for en_US.UTF-8 locale?](https://stackoverflow.com/a/63454192/995714) – phuclv Sep 05 '20 at 23:24