#include <stdio.h> #include "string.h" int N,i; FILE *fp; struct student {char num[10]; char name[8]; char sex[5]; int age; char addr[15]; int score; }stu[100]; void input() /*输入学生数据的资料*/ {printf("Input the student data %d:\n",i+1); /*输入一个学生的数据*/ printf("NO.:"); scanf("%s",stu[i].num); /*输入号数*/ printf("name:"); scanf("%s",stu[i].name); /*姓名*/ printf("sex:"); scanf("%s",stu[i].sex); /*性别*/ printf("age:"); scanf("%d",&stu[i].age); /*年龄*/ printf("address:"); scanf("%s",stu[i].addr); /*家庭地址*/ printf("score:"); scanf("%d",&stu[i].score); /*分数*/ printf("\n"); } void add() /*在ouru的文件后添加学生数据*/ { if((fp=fopen("ouru","ab"))==NULL) /*以读写的二进制形式打开ouru文件*/ {printf("Cann't open the file\n"); return;} printf("How many data do you want:"); /*输入需要添加几个学生的数据和具体的资料*/ scanf("%d",&N); for(i=0;i<N;i++) /*向ouru文件中加入添加学生的数据的个数*/ input(); for(i=0;i<N;i++) if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File error\n"); fclose(fp); } void save() /*保存学生的数据到ouru文件中*/ { int i; if((fp=fopen("ouru","wb"))==NULL) /*打开ouru文件*/ {printf("Cann't open the file\n"); return;} for(i=0;i<N;i++) /*把学生的数据写入到ouru 文件中*/ if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf("File write error\n"); fclose(fp); } void insert() /*在ouru文件中插入学生的数据*/ {char positions[10]; int a,b; if((fp=fopen("ouru","r+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++); /*读取文件并计算文件中有多少个学生的数据*/ printf("Which position do you want to insert:"); scanf("%s",positions); for(a=0;a<i;a++) /*确定插入的位置*/ if(strcmp(positions,stu[a].num)==0)break; a=a+1; fseek(fp,sizeof(struct student)*(a+1),0); /*将指针指向要插入位置的后一位*/ for(b=a;b<i;b++) fwrite(&stu[b],sizeof(struct student),1,fp); /*将插入后的学生数据向后移动一位*/ i=a; input(); fseek(fp,sizeof(struct student)*a,0); /*将指针指向要插入的位置*/ fwrite(&stu[a],sizeof(struct student),1,fp); /*将插入的数据保存到ouru文件里*/ fclose(fp);} void change() /*修改ouru文件中的某个学生的数据*/ { char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开二进制ouru文件*/ {printf("Can not open the file\n"); return;} printf("What data do you want to modify:"); /*输入要修改学生的号码*/ scanf("%s",positions); for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) /*找出要修改的数据*/ if(strcmp(stu[i].num,positions)==0) {fseek(fp,sizeof(struct student)*i,0); /*将指针指向要修改的资料*/ input(); fwrite(&stu[i],sizeof(struct student),1,fp); /*将修改的数据保存*/ break;} fclose(fp); } void del() /*删除ouru中的某个学生的数据*/ {int a,b,f; char positions[10]; if((fp=fopen("ouru","rb+"))==NULL) /*以读写打开ouru文件*/ {printf("Can not open file!"); return;} printf("Input the deleted number:"); /*输入要删除的学生号码*/ scanf("%s",positions); for(i=0;fread(&stu[i],sizeof(struct student),1,fp)!=0;i++); /*查找文件中有多少个学生的数据*/ f=i; for(a=0;a<i;a++) /*找出要删除的学生的数据并把后面的数据全都向前移一位*/ {if(strcmp(positions,stu[a].num)==0) { for(b=a;b<i;b++) {strcpy(stu[b].num,stu[b+1].num); strcpy(stu[b].name,stu[b+1].name); strcpy(stu[b].sex,stu[b+1].sex); stu[b].age=stu[b+1].age; strcpy(stu[b].addr,stu[b+1].addr); stu[b].score=stu[b+1].score;} f=f-1; break;} } fclose(fp); fopen("ouru","w"); /*将删除后的结果保存到ouru文件里*/ for(a=0;a<f;a++) fwrite(&stu[a],sizeof(struct student),1,fp); fclose(fp);} void examine() /*查看ouru文件中的资料*/ {int i; if((fp=fopen("ouru","r"))==NULL) /*以只读的方式打开ouru文件*/ {printf("Cannot open the file\n"); return;} printf("\n"); printf("num name sex age addr score\n"); /*读取各个学生数据的同时把它们显示出来*/ for(i=0;(fread(&stu[i],sizeof(struct student),1,fp))!=0;i++) {printf("%-10s%-10s%-8s%-9d%-17s%d\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].age,stu[i].addr,stu[i].score);} printf("\n"); fclose(fp); } main() {int choose; /*显示主界面*/ printf("********************************************************************************\n"); printf("--------------------------------------------------------------------------------\n\n"); printf(" Welcome to the student system\n"); printf(" Producer:ouru\tRegistration number:03011123\n"); printf("\n\n--------------------------------------------------------------------------------\n"); printf("********************************************************************************\n"); printf("Please the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n"); scanf("%d",&choose); /*选择想要操作的选项*/ while(choose!=8) /*当选择8的时候不再循环并退出*/ { switch(choose) {case 1:printf("Please input the student total:"); /*当choose为下面的数时执行相应的功能*/ scanf("%d",&N); for(i=0;i<N;i++) input(); save();break; case 2:add();break; case 3:change();break; case 4:insert();break; case 5:del();break; case 6:examine();break; case 7:printf("\n\n\nPlease the choice:\n"); printf("\t\t\t1. Input the student data\n"); printf("\t\t\t2. Add the new studetn data\n"); printf("\t\t\t3. Change a data\n"); printf("\t\t\t4. Insert a new data\n"); printf("\t\t\t5. Delete the student data\n"); printf("\t\t\t6. Manifestation student data\n"); printf("\t\t\t7. Main menu\n"); printf("\t\t\t8. exit\n\n\n");break; default :printf("error\n");} scanf("%d",&choose); } }