我有一个UTF-8文本文件,其中包含几个符号,我想把它们改成其他符号(只限于在|(和|)之间的符号),但问题是有些符号不被视为字符,而是被视为多字符符号。(我的意思是它们不能放在'∞'中间,只能像这样“∞”,那么char *?)
这是我的文本文件:
例如:
"fgetc不能使用,因为像∞这样的字符不能被视为单个字符。如果我使用它,我将无法使用strcmp将char与每个符号(char *)进行比较,我试图将我的char转换为char *但strcmp!= 0。
非常感谢未来的帮助!
编辑:如果我在每个符号之间加一个空格,那么每个符号都会被正确更改,但是没有空格,它就不起作用,这是我正在尝试解决的问题。
这是我的文本文件:
Text : |(abc∞∪v=|)
例如:
∞ 应该改为 ¤c
∪ 改为 ¸!
= 改为 "
因此,由于一些符号(如∞和∪)是多个字符,我决定使用fscanf逐字获取所有文本。这种方法的问题在于我必须在每个字符之间加上空格...我的文件应该像这样:
Text : |( a b c ∞ ∪ v = |)
"fgetc不能使用,因为像∞这样的字符不能被视为单个字符。如果我使用它,我将无法使用strcmp将char与每个符号(char *)进行比较,我试图将我的char转换为char *但strcmp!= 0。
这是我的C代码,帮助您理解我的问题:
"#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void){
char *carac[]={"∞","=","∪"}; //array with our signs
FILE *flot,*flot3;
flot=fopen("fichierdeTest2.txt","r"); // input text file
flot3=fopen("resultat.txt","w"); //output file
int i=0,j=0;
char a[1024]; //array that will contain each read word.
while(!feof(flot))
{
fscanf(flot,"%s",&a[i]);
if (strstr(&a[i], "|(") != NULL){ // if the word read contains |( then j=1
j=1;
fprintf(flot3,"|(");
}
if (strcmp(&a[i], "|)") == 0)
j=0;
if(j==1) { //it means we are between |( and |) so the conversion can begin
if (strcmp(carac[0], &a[i]) == 0) { fprintf(flot3, "¤c"); }
else if (strcmp(carac[1], &a[i]) == 0) { fprintf(flot3,"\"" ); }
else if (strcmp(carac[2], &a[i]) == 0) { fprintf(flot3, " ¸!"); }
else fprintf(flot3,"%s",&a[i]); // when it's a letter, number or sign that doesn't need to be converted
}
else { // when we are not between |( and |) just copy the word to the output file with a space after it
fprintf(flot3, "%s", &a[i]);
fprintf(flot3, " ");
}
i++;
}
}
非常感谢未来的帮助!
编辑:如果我在每个符号之间加一个空格,那么每个符号都会被正确更改,但是没有空格,它就不起作用,这是我正在尝试解决的问题。
fgetwc()
呢? - ad absurdumfeof
(https://dev59.com/jG035IYBdhLWcg3wbPU5),将j
改成其他不同的名称,比如is_converting
或其他什么,因为j
通常是一个迭代器。 - Dellowarfread()
而不是fscanf()
。由于您正在使用带有多字节字符的UTF-8,因此您需要有一种机制来读取字节流,然后逐个处理字符并识别UTF-8流中的多字节字符。另请参见UTF8 processing C,以及这篇博客文章Using UTF-8 as the internal representation for strings in C and C++ with Visual Studio。 - Richard Chamberssetlocale(LC_ALL, "");
以及%ls
格式说明符,例如scanf("%ls",string);
。 - Richard Chambers