fork download
  1. /*
  2. http://stackoverflow.com/questions/30388085/how-to-use-utf-8-in-c-code/30395420
  3.  
  4. sudo apt-get install libunistring0 libunistring-dev
  5. gcc utf8test.c -iunistring
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistr.h>
  12. #include <assert.h>
  13.  
  14. #define SIZE 40
  15.  
  16. int main(int argc, char **argv)
  17. {
  18. uint8_t buf[SIZE + 1];
  19. uint8_t *pat = "привет мир";
  20. uint8_t str[SIZE + 2];
  21.  
  22. /* Make sure program is correctly saved as UTF-8 */
  23. assert(u8_strcmp("вход", "\320\262\321\205\320\276\320\264") == 0);
  24. assert(u8_strcmp("выход", "\320\262\321\213\321\205\320\276\320\264") == 0);
  25.  
  26. FILE *f1 = fopen("вход", "r");
  27. FILE *f2 = fopen("выход", "w");
  28.  
  29. if (f1 == 0 || f2 == 0)
  30. {
  31. perror("Failed to open one or both files"); /* use perror() */
  32. return(1);
  33. }
  34.  
  35. size_t nbytes;
  36. if ((nbytes = fread(buf, 1, SIZE, f1) > 0)
  37. {
  38. size_t nchars = u8_strlen(buf);
  39.  
  40. if (u8_strncmp(buf, pat, nbytes) == 0)
  41. {
  42. sprintf(str, "%*s\n", 1+(int)nchars, buf); /* nchars+1 length specifier for pad */
  43. fwrite(str, 1, 1+nbytes, f2); /* +1 here too */
  44. }
  45. else {
  46. fprintf(stderr, "%s: %s and %s differ\n", argv[0], buf, pat);
  47. exit(1);
  48. }
  49. }
  50. else {
  51. perror("fread");
  52. }
  53.  
  54. fclose(f1);
  55. fclose(f2);
  56.  
  57. return(0);
  58. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:4:20: fatal error: unistr.h: No such file or directory
 #include <unistr.h>
                    ^
compilation terminated.
stdout
Standard output is empty