从UTF8读取文件

时间:2017-01-31 13:16:15

标签: c utf-8

我正在尝试读取包含UTF-8格式字符的文件。

    FILE * f = fopen(argv[1], "r");
    if(f == NULL){
            printf("cannot open %s\n", argv[1]);
            exit(-1);
        }
    unsigned int c = getc();
    while(c != EOF){
        printf("%d\n", c);  // UB
        c = getchar();

    }

如何读取文件以使它们位于位?例如,它看起来像:0xA3。 现在它正在打印实际的整数。 基本上,我如何以逐位格式读取字符?

1 个答案:

答案 0 :(得分:2)

示例读取utf-8文件.....

#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
        setlocale(LC_ALL, "en_US.UTF-8");
        FILE * f = fopen(argv[1], "r");
        if(f == NULL){
                printf("cannot open %s\n", argv[1]);
                exit(-1);
        }
        wchar_t wc;
        while((wc=fgetwc(f))!=WEOF){
                //wprintf(L"%lc", wc);
                // for output as hex of uthf-8 characters
                wprintf(L"0X%X,",(wint_t) wc);
        }
        wprintf(L"\n");
        fclose(f);
        return 0;
}