我正在尝试读入一个包含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。现在它正在打印实际的整数。基本上,我如何以位格式读入字符?
发布于 2017-01-31 21:50:14
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;
}https://stackoverflow.com/questions/41958597
复制相似问题