gpt4 book ai didi

c - 如何从 C 文件中查找某个字符的编号?

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:57 25 4
gpt4 key购买 nike

我试图找出一个字母(例如,字母“b”)在文件的段落中出现的次数。我正在尝试使用数组来计算它们,但我不确定如何正确使用 if-else 语句。

目前我能做的就是:

 int main(void)
{
FILE *fin = fopen("paragraph.txt", "r");
FILE *fout = fopen("number_of_times_bandc_occurs.txt", "w");
char line[]='\0';
int b=0; int c=0;

while(fscanf("%d", &line) != EOF)
{
//Using the if-else statements here is where I'm having difficulty.
}

fclose(fout);
fclose(fin);
return 0;
}

顺便说一句,我是初学者...所以我可能需要一些额外的解释。我还应该提到该段落有多行文本。

最佳答案

编辑:我误解了你的问题。要计算一个特定的字符,你应该这样做:

int ch, countB = 0, countC = 0;
while((ch = fgetc(fp)) != EOF) {
if(ch == 'B')
countB++;
if(ch == 'C')
countC++;
}

关于c - 如何从 C 文件中查找某个字符的编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26352861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com