一个小程序,添加一条记录写入文件后,不能显示出来
程序代码:
# include <stdio.h> # include <windows.h> # include <stdlib.h> # include <string.h> struct book { int book_number; char book_name[30]; float book_price; char book_class[20]; int book_sign; }school_book[30]; void InformationSearch(); void BackStage(); bool Login(); void Initialize(); void AddItem(); void ChangeItem(); void DeletItem(); void ScanItem(); void ChangePassword(); void save(FILE * fp, int i); void main()//主函数登陆 { printf("--------请输入登陆状态-------\n"); printf("1.游客登录\n"); printf("2.管理员登陆\n"); int i; scanf("%d", &i); switch(i) { case 1: { system("cls"); Sleep(800); InformationSearch();break; } case 2: { system("cls"); Sleep(800); BackStage();break; } } } void InformationSearch()//信息查询系统 { FILE * fp; int siz, j, count = 0, i = 0; char ch; if((fp=fopen("Data", "rb+"))==NULL) { printf("打开数据文件Data错误!\n"); exit(0); } printf(">>>>>>>>>>>>>>欢迎使用图书信息查询系统<<<<<<<<<<<<<<<\n"); printf("编号 名称 价格(元) 类别\n"); for(i=0;i<30;i++) { fread(&school_book[i], sizeof(struct book), 1, fp); if(school_book[i].book_number==0) { break; } printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class); printf("\n"); } } void BackStage()//后台系统 { if(Login()) { return; } while(1) { printf("--------欢迎登陆图书后台管理系统--------\n"); printf("1.初始化 所有信息\n"); printf("2.添加 图书信息\n"); printf("3.修改 图书信息\n"); printf("4.删除 图书信息\n"); printf("5.查看 图书信息\n"); printf("6.管理员密码修改\n"); int i; scanf("%d", &i); switch(i) { case 1: { system("cls"); Sleep(800); Initialize(); break; } case 2: { system("cls"); Sleep(800); AddItem(); break; } case 3: { system("cls"); Sleep(800); ChangeItem(); break; } case 4: { system("cls"); Sleep(800); DeletItem(); break; } case 5: { system("cls"); Sleep(800); ScanItem(); break; } case 6: { system("cls"); Sleep(800); ChangePassword(); break; } } } } bool Login()//打开保存密码的文件,并验证 { FILE * fp; int count; char filename[] = "password"; char stringIn[10]; char stringOut[10]; // if((fp=fopen(filename, "rb+"))==NULL) // { // printf("这是您首次使用本系统\n默认密码为admin\n"); // fp=fopen(filename, "wb+"); // fprintf(fp, "admin"); // fclose(fp); // } while ((fp = fopen(filename, "rb+")) == NULL) { printf("这是您首次使用本系统\n默认密码为admin\n"); fp = fopen(filename, "wb+") ; fprintf(fp, "admin"); fclose(fp); } fscanf(fp, "%s", stringOut); printf("请输入密码: "); scanf("%s", stringIn); count = strcmp(stringIn, stringOut); if(count==0) return false; else return true; } void Initialize() { char ch; int i = 0; FILE * fp; fp = fopen("Data", "wb+"); do { printf("=================================\n"); printf("请输入第%d项图书信息\n", i+1); printf("图书编号: "); scanf("%d", &school_book[i].book_number); ch = getchar(); printf("图书名称: "); scanf("%s", school_book[i].book_name); ch = getchar(); printf("图书价格: "); scanf("%f", &school_book[i].book_price); ch = getchar(); printf("图书类别: "); scanf("%s", school_book[i].book_class); ch = getchar(); school_book[i].book_sign = 1; printf("=================================\n"); ++i; printf("是否继续录入?(Y/N)"); ch = getchar(); }while(ch=='y'&&'Y'); save(fp, i); fclose(fp); } void AddItem() { char ch; int i, j = 0; FILE * fp; if((fp = fopen("Data", "rb+"))==NULL) { printf("cannot open file!\n"); return; } for(i=0;i<30;i++) { fread(&school_book[i], sizeof(struct book), 1, fp); if(school_book[i].book_number==0) { break; } } fclose(fp); j = i; do { printf("=================================\n"); printf("请输入第%d项图书信息\n", i+1); printf("图书编号: "); scanf("%d", &school_book[i].book_number); ch = getchar(); printf("图书名称: "); scanf("%s", school_book[i].book_name); ch = getchar(); printf("图书价格: "); scanf("%f", &school_book[i].book_price); ch = getchar(); printf("图书类别: "); scanf("%s", school_book[i].book_class); ch = getchar(); school_book[i].book_sign = 1; printf("=================================\n"); printf("是否继续录入?(Y/N)"); ch = getchar(); ++i; }while(ch=='y'&&'Y'); if((fp = fopen("Data", "ab+"))==NULL) { printf("cannot open file!\n"); return; } for(;j<i;j++) { if(fwrite(&school_book[j], sizeof(struct book), 1, fp)!=1) { printf("Error!\n"); break; } } fclose(fp); } void ChangeItem() { FILE * fp; int siz, h, j, i = 0; char ch; if((fp=fopen("Data", "rb+"))==NULL) { printf("打开数据文件Data错误!\n"); exit(0); } printf("编号 名称 价格(元) 类别\n"); for(i=0;i<30;i++) { fread(&school_book[i], sizeof(struct book), 1, fp); if(school_book[i].book_number==0) { break; } printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class); printf("\n"); } fclose(fp); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf("请输入要修改的记录序号: "); scanf("%d", &j); fp = fopen("Data", "wb+"); for(h=0;h<=i;h++) { if(school_book[h].book_number==j) { printf("=================================\n"); printf("请输入第%d项图书信息\n", j); printf("图书编号: "); scanf("%d", &school_book[h].book_number); ch = getchar(); printf("图书名称: "); scanf("%s", school_book[h].book_name); ch = getchar(); printf("图书价格: "); scanf("%f", &school_book[h].book_price); ch = getchar(); printf("图书类别: "); scanf("%s", school_book[h].book_class); ch = getchar(); school_book[h].book_sign = 1; printf("=================================\n"); } if(fwrite(&school_book[h], sizeof(struct book), 1, fp)!=1) { printf("Error!"); break; } } save(fp, i); fclose(fp); } void DeletItem() { FILE * fp; int siz, h, j, i = 0; char ch; if((fp=fopen("Data", "rb+"))==NULL) { printf("打开数据文件Data错误!\n"); exit(0); } printf("编号 名称 价格(元) 类别\n"); for(i=0;i<30;i++) { fread(&school_book[i], sizeof(struct book), 1, fp); if(school_book[i].book_number==0) { break; } printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class); printf("\n"); } fclose(fp); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); printf("请输入要删除的记录序号: "); scanf("%d", &j); fp = fopen("Data", "wb+"); for(h=0;h<=i;h++) { if(school_book[h].book_number==j) { continue; } if(fwrite(&school_book[h], sizeof(struct book), 1, fp)!=1) { printf("Error!"); break; } } save(fp, i); fclose(fp); } void ScanItem() { FILE * fp; int siz, j, count = 0, i = 0; char ch; if((fp=fopen("Data", "rb+"))==NULL) { printf("打开数据文件Data错误!\n"); exit(0); } printf("编号 名称 价格(元) 类别\n"); for(i=0;i<30;i++) { fread(&school_book[i], sizeof(struct book), 1, fp); if(school_book[i].book_number==0) { break; } printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class); printf("\n"); } } void ChangePassword() { FILE * fp; int count; char filename[] = "password"; char ch[10]; char stringIn[10]; char stringOut[10]; fp = fopen(filename, "rb+"); fscanf(fp, "%s", stringOut); printf("请输入旧密码: "); scanf("%s", stringIn); count = strcmp(stringIn, stringOut); if(count==0) { printf("密码验证成功\n新密码: "); scanf("%s", ch); fp = fopen(filename, "wb+") ; fprintf(fp, ch); fclose(fp); } else { printf("密码验证失败\n"); return; } } void save(FILE * fp, int i) { // fprintf(fp, "%d%10s%6.2f%10s", book[i].book_number, book[i].book_name, book[i].book_price, book[i].book_class); // fprintf(fp, "\n"); int j; for(j=0;j<=i;j++) { if(fwrite(&school_book[j], sizeof(struct book), 1, fp)!=1) { printf("error!"); break; } } }