#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char c,filename[20];
printf("請輸入文字檔檔名:");
scanf("%s",filename);
FILE *fp;
if ((fp=fopen(filename,"r"))==NULL) {
printf("File can't be opened!\n");
exit(0);
}
printf("文字檔內容如下:\n");
while((c=fgetc(fp))!=EOF) printf("%c",c);
fseek(fp,0,SEEK_SET);
int spos=0, count=0, slen;
char search[20],cmp[20];
printf("請輸入欲搜尋的文字:");
scanf("%s",search);
slen=strlen(search);
while(fgets(cmp,slen+1,fp)!=NULL)
{
if(!strcmp(cmp,search))
{
count++;
spos+=slen;
}
else
spos++;
fseek(fp,spos,SEEK_SET);
}
printf("%s共出現%d次\n",search,count);
}
--
◎ [1;31m龍 [32m貓 [33m資 [34m訊 [35m天 [36m地 [0m( [1mbbs.mgt.ncu.edu.tw [0m)
◎[ [1;33;46mbrucetsao [0m]From: 140.115.82.88