我写了关于文件里单词出现的次数,但是得不到结果!为什么呢??
源程序如下:
#include<stdio.h>
#include<string.h>
void main(int argc, char* argv[])
{ char word_1[10];
char word_2[10];
char ch;
int count;
FILE *fp;
if((fp=fopen("E:/file.txt","rt"))==NULL)
{ printf("cannot open the file\n");
return;}
printf("input the word:\n");
scanf("%s",word_2);
ch=fgetc(fp);
while(ch!=EOF)
{ int i=0;
while(ch!=' ')
{ word_1[i]=ch;
ch=fgetc(fp);
i++;}
word_1[i]='\0';
if((strlen(word_1)==strlen(word_2))&&(strcmp(word_1,word_2)==0))
count++;}
printf("%d",count);
fclose(fp);
}