void addRecord(void) { int i; char str[10]; int val=0,unval=0; char s[3]; FILE *fp; if(numApps==0) printf("原来没有记录,现在建立新的记录\n"); else printf("下面在当前文件添加新的记录\n"); while(1) { printf("确定要添加一组新设备信息?(Y/N)"); gets(s); if(s[0]=='n'||s[0]=='N') //不再添加信息 break; if(numApps>=Record_arraySize) //数组空间不足,需要重新申请空间 { records=realloc(records,(Record_arraySize+INCR_SIZE)*sizeof(AppInfo)); if(records==NULL) { printf("memory failed!"); exit(-1); } Record_arraySize=Record_arraySize+INCR_SIZE; } printf("\n\t请输入设备仪器的信息\n"); printf("\t编号:"); gets(records[numApps].number); printf("\t名称:"); gets(records[numApps].name); printf("\t单价:"); gets(str); records[numApps].price=(float)atof(str); printf("\t购进日期:"); gets(records[numApps].inDate); printf("\t生产厂家: "); gets(records[numApps].producter); printf("\t购进数量: "); gets(str); records[numApps].count=(int)atoi(str); while(Usability_arraySize { Usability_arraySize+=INCR_SIZE; } printf("\t仪器设备的可用性(Y)与不可用性(N):\n"); for(i=1;i<=records[numApps].count;i++) { printf("\t第%d台的可用性:",i); gets(s); records[numApps].usability[i-1]=s[0]; } numApps++; printf("\n是否继续添加新的设备信息?(Y/N)"); gets(s); if(s[0]=='n'||s[0]=='N') break; } printf("现在一共有%d条记录\n",numApps); printf("存文件……\n"); fp=fopen("app_info.txt","w"); for(i=0;i { fwrite(&records[i],sizeof(AppInfo),1,fp); } fclose(fp); printf("%d条记录已经存入文件!\n",numApps); } 下面是我保存文件出现的对话框