How to use UTF-8 in C code?

后端 未结 5 979
耶瑟儿~
耶瑟儿~ 2021-02-04 03:06

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 \'привет мир\'          


        
5条回答
  •  甜味超标
    2021-02-04 03:49

    The following code works as required:

    #include 
    #include 
    #include 
    #include 
    
    #define SIZE 10
    
    int main(void)
    {
      setlocale(LC_ALL, "");
      wchar_t buf[SIZE+1];
      wchar_t *pat = L"привет мир";
      wchar_t str[SIZE+2];
    
      FILE *f1;
      FILE *f2;
    
      f1 = fopen("/tmp/вход","r");
      f2 = fopen("/tmp/выход","w");
    
      fgetws(buf, SIZE+1, f1);
    
      if (wcsncmp(buf, pat, SIZE) == 0) {
        swprintf(str, SIZE+2, L"% 11ls", buf);
        fputws(str, f2);
      }
    
      fclose(f1);
      fclose(f2);
    
      exit(0);
    }
    

提交回复
热议问题