#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 40
int main()
{
int i,count = 0;
char *cSource,*cSearch;
FILE *fp;
cSource = (char *)malloc(N * sizeof(char));
cSearch = (char *)malloc(3 * sizeof(char));
if((fp = fopen("word.txt", "r")) == NULL)
{
printf("文件打开失败!\n");
exit(0);
}
printf("输入统计的汉字:");
scanf("%s", cSearch);
fgets(cSource, N, fp);
for(i = 0; i < (int)strlen(cSource); i++)
{
if(cSource[i] == cSearch[0] && cSource[i+1] == cSearch[1])
//一个汉字占两个字节 所以需要判断两个字节的内容
count++;
}
printf("%d\n", count);
return 0;
}
在TXT文本中查找特定汉字,总是输出0不知道哪里错了
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
技术专家团-小桥流水 2022-04-12 13:58关注fgets读取一行,你的文件中只有1行吗?另外,这一行中只有汉字,没有别的字符吗,这么比较是不对的
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用