在一个文件中保存许多名字,每个姓名占一行。编写程序,从键盘输入该文件的文件名和一个名字,判断该姓名在文件中是否存在

在一个文件中保存许多名字,每个姓名占一行。编写程序,从键盘输入该文件的文件名和一个名字,判断该姓名在文件中是否存在

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char ch[1000],*i;
int n;
FILE *fp;
char name[10],fname[20];
printf("请输入文件名:");
gets(fname);
printf("请输入姓名:");
gets(name);
if((fp=fopen(fname,"r"))==NULL)
{printf("不能打开文件!\n");exit(0);}
while(!feof(fp))
{
if(fgets(ch,sizeof(ch),fp)!=NULL)
{
i=strstr(ch,name);
if(i)printf("Yes!\n");break;
}
}
fclose(fp);
if(i==NULL)printf("No!\n");
}