main()
{
int i;
FILE *fp;
char filename[50];
printf("file name!\n");
scanf("%s",&filename);
char str[10]="i love u";
char str1;
if((fp=fopen(filename,"rb"))==NULL) {
printf("Error opening source file!\n");
return -1;
}
i=0;
do {
// Read up to BLOCK_SIZE bytes from source file.
str1=fgetc(fp);
if(ferror(fp)) {
printf("Error reading data!\n");
return -1;
}
if (i!=8)
{if(str1==str[i])
i=i+1;
else
i=0;}
else
{
printf("find it");
return 0;
}
} while(!feof(fp));
return 0;
}
试试看,如何?
{
int i;
FILE *fp;
char filename[50];
printf("file name!\n");
scanf("%s",&filename);
char str[10]="i love u";
char str1;
if((fp=fopen(filename,"rb"))==NULL) {
printf("Error opening source file!\n");
return -1;
}
i=0;
do {
// Read up to BLOCK_SIZE bytes from source file.
str1=fgetc(fp);
if(ferror(fp)) {
printf("Error reading data!\n");
return -1;
}
if (i!=8)
{if(str1==str[i])
i=i+1;
else
i=0;}
else
{
printf("find it");
return 0;
}
} while(!feof(fp));
return 0;
}
试试看,如何?
main()
{
int i;
FILE *fp;
char filename[50],str[10]="i love u",str1;
printf("file name!\n");
scanf("%s",&filename);
if((fp=fopen(filename,"rb"))==NULL) {
printf("Error opening source file!\n");
return -1;
}
i=0;
do {
str1=fgetc(fp);
if(ferror(fp)) {
printf("Error reading data!\n");
return -1;
}
if (i!=8)
{if(str1==str[i])
i=i+1;
else
i=0;}
else
{
printf("find it");
return 0;
}
} while(!feof(fp));
printf("not find it");
return 0;
}
{
int i,j;
FILE *fp;
char filename[50],str[10]="i love u",str1;
printf("file name!\n");
scanf("%s",&filename);
if((fp=fopen(filename,"rb"))==NULL) {
printf("Error opening source file!\n");
return -1;
}
i=0;
j=0;
do {
str1=fgetc(fp);
if(ferror(fp)) {
printf("Error reading data!\n");
return -1;
}
if (i!=8)
{if(str1==str[i])
i=i+1;
else
i=0;}
else
{
printf("find it\n");
i=0;
j=j+1;
}
} while(!feof(fp));
if(j==0)
printf("not find it");
else
printf("j=%d",j);
return 0;}该程序不但可以判断有没有你所查找的字符串,而且还可以统计所查找到的字符串的总数