#include “stdio.h”#include “string.h”#include “stdlib.h”#include “conio.h”#include “windows.h"int i=0,j=0,current_stdnum; //定义数组行列struct student //定义结构体{ int stdnum; char name[20]; int score1; int score2; int score3;}stu[10];void savestudents(); //声明void checkstudents();void changestudents();void insertstudents();void cleanstudents();void rankstudents();void displaymenu() //展示菜单{ printf(”\n 学生成绩管理系统 \n\n"); printf("\n* 1.录入学生成绩 \n"); printf("\n 2.查询学生成绩 \n"); printf("\n 3.修改学生成绩 \n"); printf("\n 4.初始化系统 \n"); printf("\n 5.成绩排序 \n"); printf("\n 6.成绩插入 \n"); printf("\n 7.数据统计 \n"); printf("\n 8.记录浏览 \n"); printf("\n 0.Exit *\n"); int choice; printf(“输入相应数字,进入相应功能:\n”); scanf("%d",&choice); switch(choice) { case 1:savestudents(); case 2:checkstudents(); case 3:changestudents(); case 4:cleanstudents(); //case 5:rankstudents(); case 6:insertstudents(); case 7: case 8: case 0:exit(0); }}void cleanstudents(){ int choice; printf("=提示==\n\n 是否将系统初始化 \n\n Yes=1 || No0 \n\n========================"); scanf("%d",&choice); if(choice!=1) { printf(“已取消系统初始化!”); } else { FILE *fp; fp = fopen(“account.dat”, “wb”); fclose(fp); printf(“系统初始化完毕,数据已被清空!\n请重启程序!”); system(“pause”); exit(0); } }void insertstudents() //插入学生成绩{ FILE *fp; fp = fopen(“account.dat”, “ab”); do { i++; printf(“学生成绩插入:\n\n”); printf(“学号: “); scanf(”%d”,&stu[i].stdnum); printf(“姓名:\n”); scanf("%s",stu[i].name); printf(“数学:\n”); scanf("%d",&stu[i].score1); printf(“语文:\n”); scanf("%d",&stu[i].score2); printf(“英语:\n”); scanf("%d",&stu[i].score3); if(fwrite(&stu[i],sizeof(student),1,fp)!=1) { printf(“数据写入失败!”); } printf(“是否继续插入学生成绩?\n【继续请按T】”); }while(getch()‘T’||getch()‘t’); fclose(fp); displaymenu();}void changestudents() //修改数据{ FILE *fp; if((fp=fopen(“account.dat”,“rb”))NULL) { printf(“文件打开失败!”); } printf(“请输入要修改的学生学号\n”); scanf("%d",¤t_stdnum); fread(&stu[i],sizeof(student),2,fp); fclose(fp); fp=fopen(“account.dat”,“wb”); for(i=0;i<10;i++) { if(stu[i].stdnum == current_stdnum) { printf(“当前学生信息(学号无法修改!):\n学号:%d\n姓名:%s\n数学:%d分\n语文:%d分\n英语:%d分\n\n请按照顺序依次输入!!”,stu[i].stdnum,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3); scanf("%s %d %d %d",stu[i].name,&stu[i].score1,&stu[i].score2,&stu[i].score3); break; } } if(fwrite(&stu[i],sizeof(student),1,fp)!=1) { printf(“数据写入失败!”); } fclose(fp); displaymenu();}void checkstudents() //查询学生成绩{ FILE *fp; if((fp=fopen(“account.dat”,“rb”))NULL) { printf(“文件打开失败!”); } printf(“输入查询学生的学号\n”); scanf("%d",¤t_stdnum); for(i=0;i<10;i++) { fread(&stu[i],sizeof(student),1,fp); if(stu[i].stdnum == current_stdnum) { printf("\n学号:%d\n姓名:%s\n数学:%d分\n语文:%d分\n英语:%d分\n",stu[i].stdnum,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3); break; } } fclose(fp); displaymenu();}void savestudents() //录入学生成绩{ FILE *fp; fp = fopen(“account.dat”, “wb”); do { i++; printf(“学生成绩录入:\n\n”); printf(“学号: “); scanf(”%d”,&stu[i].stdnum); printf(“姓名:\n”); scanf("%s",stu[i].name); printf(“数学:\n”); scanf("%d",&stu[i].score1); printf(“语文:\n”); scanf("%d",&stu[i].score2); printf(“英语:\n”); scanf("%d",&stu[i].score3); if(fwrite(&stu[i],sizeof(student),1,fp)!=1) { printf(“数据写入失败!”); } printf(“是否继续录入学生成绩?\n【继续请按T】”); }while(getch()‘T’||getch()‘t’); fclose(fp); displaymenu();}int main(){ displaymenu(); return 0;}
新手写的这个东西,出现只要程序内修改了数据,其他数据就无法读取的情况,怎么办
最新推荐文章于 2021-07-01 10:14:49 发布