我有一个用C编写的程序,它应该计算单词"the"出现在作为参数给出的文本文件中的次数.但程序不断给出分段错误错误,我对如何解决这个问题没有更多的想法.任何帮助将不胜感激.谢谢!
这是代码:
#include <stdio.h>
#include <string.h>
void main(int argc, char *argv[])
{
int h,i;
FILE *fp;
char* mess;
for(i=1; i < argc; i++)
{
h=0;
fp=fopen(argv[i],"r");
while (!feof(fp))
{
fscanf(fp,"%s",mess);
if (strcmp(mess,"the")==0)
h++;
}
printf("The file %s contains the word \"the\" %d times.",argv[i],h);
h=0;
fclose(fp);
}
}