#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME_MAX 20
int StrCount(FILE *file,char *str);
void main()
{
char *filename,*spestr;
int num;
FILE *fp;
filename=(char *)malloc(FILENAME_MAX);
spestr=(char *)malloc(FILENAME_MAX);
printf("Input the filename:");
while(1)
{
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp!=NULL)break;
printf("Can't open the file.");
}
printf("Input the special string:");
scanf("%s",spestr);
num=StrCount(fp,spestr);
fclose(fp);
printf("%d times of %s in %s.",num,spestr,filename);
}
int StrCount(FILE *file,char *str)
{
int i,length,count=0;
char ch,q,*temp;
char s[20];
q=*str;
length=strlen(str);
while(1)
{
while((ch=fgetc(file))!=EOF)
{
if(ch==q)
continue;
}
for(i=0;i<length;i++)
{
ch=fgetc(file);
s[i]=ch;
}
if(strcmp(str,s)==0)
count+=1;
if(ch==EOF)break;
}
return count;
}
运行出来貌似不行啊,strcount这个函数怎么改,对文件中的定位真是晕