简单的姓名号码查询系统

2017-01-07 11:16:07来源:cnblogs.com作者:legenda人点击

第七城市
 1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 struct student    //定义结构体 5 { 6     char name[7];   //姓名 7     int number;     //号码 8 }student,student1; 9 void menu()    //显示栏10 {11     printf("***********************/n");12     printf("1 input record/n");13     printf("2 search record/n");14     printf("0 quit system/n");15 }16 void save()  //输入函数17 {18    FILE *fp;19     system("cls");  //清屏20     fp=fopen("filename","a+");21     if(fp==NULL)22     {23         printf("open file error");24         exit(1);25     }26     while(1)27     {28         scanf("%s%d",student.name,&student.number);   //输入姓名及号码29         fwrite(&student,sizeof(struct student),1,fp);30         printf("continue?(y/n)");  //是否继续31         getchar();32         if(getchar()=='n')  break;33     }34     fclose(fp);35     exit(0);36 }37 void search()//查找函数38 {39 40      FILE *fp;41      char name[7];42     system("cls");//清屏43     if((fp=fopen("filename","r"))==NULL)44     {45         printf("open the file error");46         exit(1);47     }48     printf("please int your name/n");49     scanf("%s",name);50     while(fread(&student1,sizeof(struct student),1,fp)==0)//查找51     {52         if(strcmp(student1.name,name)==0)53         {54             printf("%s  %d",student1.name,student1.number);//显示所查找的姓名以及号码55             break;56         }57     }58     if(feof(fp))//检测文件指针指向文件是否结束59     {60         printf("search fail");//若结束则输出查找失败61         exit(0);62     }63 }64 int main()//主函数65 {66     int select;67     menu();//显示主菜单68     printf("/nPlease enter your select/n");69     scanf("%d",&select);70     while(select)71     {72         switch(select)73         {74             case 1:save();break;75             case 2:search();break;76             case 0:exit(1);77         }78     }79 }

 

第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台