我的程序出错了,两个问题,一是读取的文件循环两次,还有就是查找文件直接崩溃
程序代码:
/* 要实现的功能 1、对疾病列表的读取 2、对序号 id 症状和科室的查询 */ # include <stdio.h> #include<stdlib.h> # include<conio.h> # include<string.h> struct Illness /*声明疾病列表的数据结构体*/ { int number; //序号 char id[10]; //id编号 char symptom[20]; //症状 char department[20]; //科室 }; struct Illness illness1[3000]; void list(); void read(); void find(); void find_number(); void find_id(); int n=-1; int main() { char ch; read(); //int a;printf("%s",illness1[a].number); // 测试读取文件是否赋值到数组专用!!!! while(1) { system("cls"); /******清屏专用******/ printf(" 按‘1’查询信息\n 按‘2’输出所有信息\n 按‘0’结束\n"); ch=getche(); if(ch>'2'||ch<'0') { printf("\n\t\t输入有误,请按任意键返回重新选择(0-2)"); getch(); continue; } switch(ch) { case '1':find();break; case '2':list();break; case '0':exit(0); default:printf("\n\t\t\t请重新选择(0-2)");break; } getche(); } return 0; } void list() { int i; //用做循环 system("cls"); /******清屏专用******/ //需要stdlib.h头文件 printf("\n\n\t\t\t 所有疾病列表如下\n"); printf("\t\t\t========================\n\n"); printf("%d%s%s%s","序号","id","症状","科室"); i = 0; while(i < 2732) { i++; printf("%d",illness1[i].number); printf("%s",illness1[i].id); printf("%s",illness1[i].symptom); printf("%s",illness1[i].department); printf("\n"); getch(); break; } printf("按任意键返回主菜单……"); getch(); return; } void read() { FILE *jb; if((jb=fopen("jibing.txt","r"))==NULL) { printf("\n\n\t疾病列表无法打开!"); printf("\n\n\t按任意键进入主菜单......"); getch(); return; } else { jb = fopen("jibing.txt","r"); n++; while(fread(&illness1[n],sizeof(illness1[n]),3000,jb)==1) fclose(jb); printf("\n\n\t疾病列表读取完毕\n",n); } } void find() { int i; printf("\n\t\t请选择(1-2)\n"); printf("\t\t\t1.表示按序号查询\n"); printf("\t\t\t2.表示按id编号查询\n"); printf("\n\t\t你的选择是:"); scanf("%d",&i); switch(i) { case 1:find_number();break; // case 2:find_id();break; } if(i !=1&&i !=2) { printf("\n\t\t输入有误,请按任意键返回重新选择(1-2)"); getch(); } getch(); } void find_number() { int number; int x; //循环变量 printf("\t请输入序号:"); //提示信息 scanf("%d",&number); //从键盘读取字符串 for(x=0;x<=n;x++) { if(strcmp(illness1[x].number,number)==0)//比较并找到 { printf("\n该序号情况如下:\n"); printf("=======================\n\n"); printf("%-6d%-6s%-9s%-9s","序号","id","症状","科室"); printf("%-6d%-6s%-9s%-9s",illness1[x].number,illness1[x].id,illness1[x].symptom,illness1[x].department); getch(); break; } } }