user-0 提问: 将包含个人信息的结构写入到文件中后，输入一个名字（英文），怎么在文件中查找该姓名对应的结构？

回复:
user: user-1
content: #include "stdio.h" #include "ctype.h" #include "stdlib.h" #include "stdbool.h" #include "string.h" #define __STDC_WANT_LIB_EXT1__1 #define NAME_MAX 20 #define BUFSIZE 1000 typedef struct Student Student; typedef struct Birth Birth;        struct Birth { int day; int month; int year; }dob; struct Student { char name[NAME_MAX]; char stdnum[20]; Birth dob;//the student's birthday int sex;//1 for girls and 0 for boys char num[11];//the student's phone number }Amy; bool get_person(Student *pAmy);//input information about a student void find(void);//function used to find the student  int main(void) { char *filename="myfile.text";//The name of file that will be used later(a string) FILE *pfile=NULL;         //Pointer to the file     pfile=fopen(filename,"wb+");             //修改 if(pfile==NULL)     {     	printf("Unable to open this file.");     	exit(1);     }     setvbuf(pfile,NULL,_IOFBF,BUFSIZE); Student member;     //只要有信息读入，就写入文件     while(get_person(&member))//get_person函数返回布尔类型值       fwrite(&member,sizeof(struct Student), 1,pfile);     //输入完信息后，关闭文件     fclose(pfile);pfile=NULL;     //程序应用：选择一个学生，显示其信息与其所有的室友电话号码     find();     fclose(pfile);pfile=NULL;     return 0; } bool get_person(Student *temp) { static char more='\0'; printf("Do you want to enter details of %s student (Y or N)?",                                         more!='\0'?"another":"a"); scanf(" %c",&more); if(tolower(more)=='n')    return false; printf("Enter the name of the student:"); scanf("%s",temp->name);     printf("Enter the student number of the student:"); scanf("%s",temp->stdnum); printf("Enter %s's birthday(day month year):",temp->name); scanf(" %d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year); //检验输入的日期是否合理 if((temp->dob.year%4==0&&temp->dob.year%100!=0)||(temp->dob.year%400==0))   {if(temp->dob.month==2&&temp->dob.day>29)    printf("In the Febuary in a leap yaer,that day doesn't exist.");} else   {if(temp->dob.month==2&&temp->dob.day>28)    printf("In the Febuary in a common yaer,that day doesn't exist.");} if((temp->dob.month==1||temp->dob.month==3||temp->dob.month==5||temp->dob.month==7||temp->dob.month==8||temp->dob.month==10||temp->dob.month==12)&&(temp->dob.day>31))        printf("In this month,that day doesn't exist."); if((temp->dob.month==4||temp->dob.month==6||temp->dob.month==9||temp->dob.month==11)&&(temp->dob.day>30))        printf("In this month,that day doesn't exist."); //继续读入其他信息 printf("Enter %s's sex(1 for girls and 0 for boys):",temp->name); scanf("%d",&temp->sex); printf("Enter %s's phone number:",temp->name); scanf("%s",temp->num); return true; } void find(void) {     char *filename="myfile.text"; FILE *pfile=NULL;              pfile=fopen(filename,"rb+"); if(pfile==NULL)     {     	printf("Unable to open this file.");     	exit(2);     }     setvbuf(pfile,NULL,_IOFBF,BUFSIZE);     Student member;     int a,b=0;     char ch[20]; printf("Choose a way in which you can find the student's information(1 for student number and 2 for name):");     scanf("%d",&a);      if(a==1)    {        printf("Enter the student's student number whom you want to contact:");     scanf("%s",ch);     while(fread(&member,sizeof(struct Student),1,pfile)==1)        if(strcmp(member.stdnum,ch)==0)        {           b++;          printf("%s is born on %d/%d/%d/,%s phone number is %s.",member.name,             member.dob.day,member.dob.month,member.dob.year,member.sex==1?"his":"her",member.num); 		}     if(b==0) printf("Sorry,no such student.");    } else if(a==2)    {   printf("Enter the student's name whom you want to contact:");        scanf("%s",ch);                          while(fread(&member,sizeof(struct Student),1,pfile)==1)          if(strcmp(member.name,ch)==0)          {            b++;            printf("%s is born on %d/%d/%d/,%s phone number is %s.",member.name,             member.dob.day,member.dob.month,member.dob.year,member.sex==1?"his":"her",member.num);          }     if(b==0) printf("Sorry,no such student.");     }  }  只能查程序运行时写入文件的信息

user: user-0
content: #include "stdio.h" #include "ctype.h" #include "stdlib.h" #include "stdbool.h" #include "string.h" #define __STDC_WANT_LIB_EXT1__1 #define NAME_MAX 20 #define BUFSIZE 1000 typedef struct Student Student; typedef struct Birth Birth;        struct Birth { 	int day; 	int month; 	int year; }dob; struct Student { 	char name[NAME_MAX]; 	char stdnum[20]; 	Birth dob;//the student's birthday 	int sex;//1 for girls and 0 for boys 	char num[11];//the student's phone number }Amy; bool get_person(Student *pAmy);//input information about a student void find(void);//function used to find the student  int main(void) { 	char *filename="myfile.text";//The name of file that will be used later(a string) 	FILE *pfile=NULL;         //Pointer to the file     pfile=fopen(filename,"wb"); 	if(pfile==NULL)     {     	printf("Unable to open this file.");     	exit(1);     }     setvbuf(pfile,NULL,_IOFBF,BUFSIZE); 	Student member;     //只要有信息读入，就写入文件     while(get_person(&member))//get_person函数返回布尔类型值       fwrite(&member,sizeof member, 1,pfile);     //输入完信息后，关闭文件     fclose(pfile);pfile=NULL;     //程序应用：选择一个学生，显示其信息与其所有的室友电话号码     void find(void);     fclose(pfile);pfile=NULL;     return 0; } bool get_person(Student *temp) { 	static char more='\0'; 	printf("Do you want to enter details of %s student (Y or N)?", 	                                        more!='\0'?"another":"a"); 	scanf(" %c",&more); 	if(tolower(more)=='n') 	   return false; 	printf("Enter the name of the student:"); 	scanf("%s",temp->name);     printf("Enter the student number of the student:"); 	scanf("%s",temp->stdnum); 	printf("Enter %s's birthday(day month year):",temp->name); 	scanf(" %d %d %d",&temp->dob.day,&temp->dob.month,&temp->dob.year); 	//检验输入的日期是否合理 	if((temp->dob.year%4==0&&temp->dob.year%100!=0)||(temp->dob.year%400==0)) 	  {if(temp->dob.month==2&&temp->dob.day>29) 	   printf("In the Febuary in a leap yaer,that day doesn't exist.");} 	else 	  {if(temp->dob.month==2&&temp->dob.day>28) 	   printf("In the Febuary in a common yaer,that day doesn't exist.");} 	if((temp->dob.month==1||temp->dob.month==3||temp->dob.month==5||temp->dob.month==7||temp->dob.month==8||temp->dob.month==10||temp->dob.month==12)&&(temp->dob.day>31)) 	       printf("In this month,that day doesn't exist."); 	if((temp->dob.month==4||temp->dob.month==6||temp->dob.month==9||temp->dob.month==11)&&(temp->dob.day>30)) 	       printf("In this month,that day doesn't exist."); 	//继续读入其他信息 	printf("Enter %s's sex(1 for girls and 0 for boys):",temp->name); 	scanf("%d",&temp->sex); 	printf("Enter %s's phone number:",temp->name); 	scanf("%s",temp->num); 	return true; } void find(void) {     char *filename="myfile.text"; 	FILE *pfile=NULL;              pfile=fopen(filename,"wb"); 	if(pfile==NULL)     {     	printf("Unable to open this file.");     	exit(2);     }     setvbuf(pfile,NULL,_IOFBF,BUFSIZE);     Student member;     int a,b=0;     char ch[20]; 	printf("Choose a way in which you can find the student's information(1 for student number and 2 for name):");     scanf("%d",&a);      if(a==1)    {        	printf("Enter the student's student number whom you want to contact:");     scanf("%s",ch);     while(fread(&member,sizeof(struct Student),1,pfile)==1)        if(strcmp(member.stdnum,ch)==0)        {           b++;          printf("%s is born on %d/%d/%d/,%s phone number is %s.",member.name,             member.dob.day,member.dob.month,member.dob.year,member.sex==1?"his":"her",member.num); 	   }     if(b==0) printf("Sorry,no such student.");    } 	else if(a==2)    {   printf("Enter the student's name whom you want to contact:");        scanf("%s",ch);                          while(fread(&member,sizeof(struct Student),1,pfile)==1)          if(strcmp(member.name,ch)==0)          {            b++;            printf("%s is born on %d/%d/%d/,%s phone number is %s.",member.name,             member.dob.day,member.dob.month,member.dob.year,member.sex==1?"his":"her",member.num);          }     if(b==0) printf("Sorry,no such student.");     }  } 好像连接上有问题。。。main不会执行find函数  把find放到里面去就执行了 但是结果是错的。。。不管有没有这个学生都输出Sorry,no such student. 求大神帮忙看看o(╥﹏╥)oo(╥﹏╥)oo(╥﹏╥)o

user: user-1
content: void find()                  {    int a,b=0;       char ch[20];        FILE *fp;           if((fp=fopen("dd.dat","rb"))==NULL)           {            printf("cannot open file\n");            exit(0);                                            }       printf("请输入要查询的成绩的方式\n\t1.编号 2.姓名:\n\t请选择 1 or 2 \t");      scanf("%d",&a);        if(a==1)      {                             getchar();        printf("请输入客户的编号:\t");        gets(ch);                                    while(fread(&customer,sizeof(struct Customer),1,fp)==1)                                    if(strcmp(customer.num,ch)==0)                                     {                                      b++;                                     printf("编号\t姓名\t年龄\t地址\t消费\t积分\t\n");                                     printf("%5s\t%5s\t%5d\t%5s\t%5.2f\t%5.2f\t\n",customer.num,customer.name,customer.age,customer.addr,customer.cost,customer.integral);                                     }                                     if(b==0) printf("对不起！没有找到相关信息：\n");      }      else if(a==2)      {                     printf("请输入成绩的姓名:\t");                            getchar();                            gets(ch);                            while(fread(&customer,sizeof(struct Customer),1,fp)==1)                            if(strcmp(customer.name,ch)==0)                            {                            b++;                            printf("编号\t姓名\t年龄\t地址\t消费\t积分\t\n");                            printf("%5s\t%5s\t%5d\t%5s\t%5.2f\t%5.2f\t\n",customer.num,customer.name,customer.age,customer.addr,customer.cost,customer.integral);                            }       if(b==0) printf("input error!\n");       }      fclose(fp);    }            仿照这个试试