Reputation: 67
I want to read a Unicode string from input and i'm using wide chars for this. When I'm inputting an ASCII string it works just fine, but when I'm inputting cyrillics the input seems to be empty.
#include <cstdio>
#include <string>
#include <cwchar>
int main() {
setlocale(LC_ALL, "rus");
wchar_t c[64];
wscanf(L"%ls", c);
wprintf(L"%d", wcslen(c));
return 0;
}
< hello
> 5
< алло
> 0
Upvotes: 1
Views: 226
Reputation: 70213
Your program does not check any return values. In this case, I would be looking askance at the return from setlocale
, because I am pretty sure that call failed and returned NULL
.
Which locales you can set is depending on which locales are actually installed on your machine.
On Linux, you can get a list of possible locales from /usr/share/i18n/SUPPORTED
:
$ grep -i ru /usr/share/i18n/SUPPORTED
ce_RU UTF-8
cv_RU UTF-8
mhr_RU UTF-8
os_RU UTF-8
ru_RU.UTF-8 UTF-8
ru_RU.KOI8-R KOI8-R
ru_RU ISO-8859-5
ru_RU.CP1251 CP1251
ru_UA.UTF-8 UTF-8
ru_UA KOI8-U
tt_RU UTF-8
You will note that it is not "rus", but "ru_RU.UTF-8". But is that locale actually installed on your machine? You can get a list of installed locales via locale -a
, and you can install a new locale by locale-gen
, which requires superuser rights.
So,
setlocale
actually succeeded.All that being said, note that you're using C functionality here. You are probably better off using C++ locale
/ <iostream>
instead of <clocale>
/ type-unsafe <cstdio>
.
Upvotes: 2