on-internet
← Community

How to use UTF-8 in C code?

Stack Overflow ·

My setup: gcc-4.9.2, UTF-8 environment. The following C-program works in ASCII, but does not in UTF-8. Create input file: echo -n 'привет мир' > /tmp/вход This is test.c: #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 10 int main(void) { char buf[SIZE+1]; char *pat = "привет мир"; char str[SIZE+2]; FILE *f1; FILE *f2; f1 = fopen("/tmp/вход","r"); f2 = fopen("/tmp/выход","w"); if (fread(buf, 1, SIZE, f1) > 0) { buf[SIZE] = 0; if (strncmp(buf, pat, SIZE) == 0) { sprintf(str, "% 11s\n", buf); fwrite(str, 1, SIZE+2, f2); } } fclose(f1); fclose(f2); exit(0); } Check the res

Comments

Sign in or sign up to comment and upvote.

No comments yet.