写了个查找字符串的程序,有些问题……
程序代码:
/*此程序用来查找某个txt内是是否有某个字符串
如果有,返回出现次数,目前的BUG是输入的文件
名或者路径不能太长。目前想增加的功能是,希望
在输入文件名的时候可以使用tab键的自动补全功能
程序版本:v1.02
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define STR_LEN 20 //数组长度
static int count=0; //统计出现的次数
int main()
{
FILE *fp;
int flag = 0;
char filename[STR_LEN], *name = filename;
char onestring[STR_LEN], twostring[STR_LEN];
while (1)
{
printf("你是想从哪个文件中查找这个文件?\n");//在哪里找
scanf("%s", filename);
if((fp = fopen(name, "r")) == NULL)
{
printf("文件不存在!");
getch();
system("cls");
}
else
{
printf("\n请输入你要查找的的字符串\n"); //要找什么
scanf("%s", onestring);
break;
}
}
while(!feof(fp))
{
fscanf(fp, "%s", twostring);
if(!strcmp(onestring, twostring))
{
count++;
}
}
if(count == 0)
{
printf("饿~没有这个字符串!");
}
else
{
printf("一共找到了%d", count);
}
fclose(fp);
return 0;
}









