You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*rewind(fp) 该函数是:将文件指针重新指向一个流的开头。
每次查询或者包含有查找属性的功能时,要用到。
不用也可以,但是不能保证查找结果的正确性。
你想:一篇文档,若查找一个字,是从头查找,还是从中间的地方开始查找准确?
若不是从头查找,也许可能会找到,但绝不是文件中的第一个。
若记录符合的全体,则会有遗漏。
所以,要加上该函数。不加的话,不知道当前指针的位置如何。若能保证指针指向开头,也可以不加
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 50
void search(){
system("cls");
struct TX{
char name[10];
char jiguan[20];
char d1[20],d2[20];
char ema[30];
}inf [SIZE];
FILE *fp;
int i,flag=0;
char a[20];
fp=fopen("date.dat","rb");//以只读方式打开二进制文件
rewind(fp); //将指针指向文件开头,从文件开头查找
printf("\t\t\t\t\t\t请输入需要查询信息的姓名:");
scanf("%s",a);
for(i=0;fread(&inf[i],sizeof(struct TX),1,fp)==1;i++){
if(strcmp(a,inf[i].name)==0){//找到对应的信息则输出
printf("\n\n\n");
printf("\t\t\t\t\t\t------------------------\t\t\t\n");
printf("\t\t\t\t\t\t姓名:%s\n",inf[i].name);
printf("\t\t\t\t\t\t籍贯:%s\n",inf[i].jiguan);
printf("\t\t\t\t\t\t电话号码1%s\n",inf[i].d1);
printf("\t\t\t\t\t\t电话号码2%s\n",inf[i].d2);
printf("\t\t\t\t\t\t邮箱:%s\n",inf[i].ema);
printf("\t\t\t\t\t\t------------------------\t\t\t\n\n\n\n\n");
flag=1;
break;
}
}if(flag==0)printf("\t\t\t\t\t\t输入错误!\n\n\n");
fclose(fp);
}