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:

Å
Ä
Ö
Γé¼
å
ä
├╢
2 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/gmes78 Apr 21 '24

OP said their environment doesn't support that.

2

u/polytopelover Apr 21 '24

Whoops, should have read more carefully. Then using a UTF-8 string library is the correct solution.