r/programminghelp • • Apr 19 '24
C Comparing characters from file with unicode characters
EDIT: Fixed it, just made 3 different character arrays, first has the first character, second has the first and second character etc. Then I just compared all the character arrays with "€".
I'm trying to read a file line by line and then compare each character in the line with another character such as €. When I run the code below it prints out 3 symbols for €, 2 symbols for åäö and correctly prints out characters such as abc. Does anyone know how to solve this? I've seen some people suggesting to use the setLocale() function but that doesn't work in my case since I'm programming a microcontroller.
FILE *file = fopen("a.txt", "r");
wchar_t c;
while ((c = fgetwc(file)) != WEOF) {
wprintf(L"%c", c);
if (c == L'\u20AC') {
printf("Found €\n");
}
}
wprintf(L"\n\u20AC\n");
fclose(file);a.txt, encoded with utf-8:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Å
Ä
Ö
€
å
ä
öOutput when I run the code, it doesn't print out € at the end:
Å
Ä
Ö
Γé¼
å
ä
├╢
View all comments
Show parent comment
1
u/polytopelover Apr 21 '24 edited Apr 21 '24
EDIT: discard my answer OP, I didn't read carefully enougn
Wide characters are not UTF-16, they are implementation and locale-dependent. You can
setlocale(LC_ALL, "C.UTF-8")and the wide character functions will then use UTF-8 for multibyte characters string representation on toolchains which support it. You might also try "en_US.UTF-8" or something similar.