我试图从文本文件中读取UTF8文本,然后将其中一些文本打印到另一个文件.我正在使用Linux和gcc编译器.这是我正在使用的代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fin;
FILE *fout;
int character;
fin=fopen("in.txt", "r");
fout=fopen("out.txt","w");
while((character=fgetc(fin))!=EOF){
putchar(character); // It displays the right character (UTF8) in the terminal
fprintf(fout,"%c ",character); // It displays weird characters in the file
}
fclose(fin);
fclose(fout);
printf("\nFile has been created...\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它现在适用于英文字符.
Jos*_*ham 15
代替
fprintf(fout,"%c ",character);
Run Code Online (Sandbox Code Playgroud)
使用
fprintf(fout,"%c",character);
Run Code Online (Sandbox Code Playgroud)
第二个fprintf()
不包含空格,之后%c
导致out.txt显示奇怪的字符.原因是fgetc()
检索单个字节(与ASCII字符相同),而不是 UTF-8字符.由于UTF-8也兼容ASCII,它会将英文字符写入文件.
putchar(character)
顺序输出字节,每个字节之间没有额外的空格,因此原始的UTF-8序列保持不变.要看看我在说什么,试试吧
while((character=fgetc(fin))!=EOF){
putchar(character);
printf(" "); // This mimics what you are doing when you write to out.txt
fprintf(fout,"%c ",character);
}
Run Code Online (Sandbox Code Playgroud)
如果要将UTF-8字符与它们之间的空格写入out.txt,则需要处理UTF-8字符的可变长度编码.
#include <stdio.h>
#include <stdlib.h>
/* The first byte of a UTF-8 character
* indicates how many bytes are in
* the character, so only check that
*/
int numberOfBytesInChar(unsigned char val) {
if (val < 128) {
return 1;
} else if (val < 224) {
return 2;
} else if (val < 240) {
return 3;
} else {
return 4;
}
}
int main(){
FILE *fin;
FILE *fout;
int character;
fin = fopen("in.txt", "r");
fout = fopen("out.txt","w");
while( (character = fgetc(fin)) != EOF) {
for (int i = 0; i < numberOfBytesInChar((unsigned char)character) - 1; i++) {
putchar(character);
fprintf(fout, "%c", character);
character = fgetc(fin);
}
putchar(character);
printf(" ");
fprintf(fout, "%c ", character);
}
fclose(fin);
fclose(fout);
printf("\nFile has been created...\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码对我有用:
/* fgetwc example */
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
int main ()
{
setlocale(LC_ALL, "en_US.UTF-8");
FILE * fin;
FILE * fout;
wint_t wc;
fin=fopen ("in.txt","r");
fout=fopen("out.txt","w");
while((wc=fgetwc(fin))!=WEOF){
// work with: "wc"
}
fclose(fin);
fclose(fout);
printf("File has been created...\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)