|
发表于 2016-1-6 23:10:48
|
显示全部楼层
- #include<stdio.h>
- #include<stdlib.h>
- #define SIZE 100
- struct Student
- {
- char name[20];
- int num;
- double score[3];
- }stu[SIZE];
- int main()
- {
- FILE *p1, *p2, *p3, *p4;
- int i = 0, n, j, t, a, b = 0, c;
- if ((p1 = fopen("stud", "wb")) == NULL)
- {
- printf("文件无法打开!\n");
- exit(0);
- }
- do
- {
- printf("***简单的成绩管理系统***\n");
- printf("\n");
- printf(" 【请选择】\n");
- printf("【1】***添加一个学生的的记录\n");
- printf("【2】***查看一个学生的记录\n");
- printf("【3】***修改一个学生的记录\n");
- printf("【4】***浏览所有学生记录\n");
- scanf("%d", &n);
- while (n != 1 && n != 2 && n != 3 && n != 4)
- {
- printf("输入错误,请重新输入:");
- scanf("%d", &n);
- }
- switch (n)
- {
- case 1:
- if ((p1 = fopen("stud", "ab")) == NULL)
- {
- printf("文件无法打开!\n");
- exit(0);
- }
- printf("请输入学生姓名:");
- scanf("%s", stu[i].name);
- printf("请输入该学生的学号:");
- scanf("%d", &stu[i].num);
- printf("请输入该学生的高数成绩:");
- scanf("%lf", &stu[i].score[0]);
- while (stu[i].score[0]<0 || stu[i].score[0]>100)
- {
- printf("高数成绩输入错误,请重新输入:");
- scanf("%lf", &stu[i].score[0]);
- }
- printf("请输入该学生的英语成绩:");
- scanf("%lf", &stu[i].score[1]);
- while (stu[i].score[1]<0 || stu[i].score[1]>100)
- {
- printf("英语成绩输入错误,请重新输入:");
- scanf("%lf", &stu[i].score[1]);
- }
- printf("请输入该学生的C++成绩:");
- scanf("%lf", &stu[i].score[2]);
- while (stu[i].score[2]<0 || stu[i].score[2]>100)
- {
- printf("C++成绩输入错误,请重新输入:");
- scanf("%lf", &stu[i].score[2]);
- }
- fseek(p1, i*sizeof(struct Student), 1);
- if (fwrite(&stu[i], sizeof(struct Student), 1, p1) != 1)
- {
- printf("输入错误!\n");
- }
- i++;
- printf("输入成功\n");
- fclose(p1);
- break;
- case 2:
- if ((p2 = fopen("stud", "rb")) == NULL)
- {
- printf("文件无法打开!\n");
- exit(0);
- }
- printf("请输入要查看的学生的学号:");
- scanf("%d", &j);
- t = i;
- while (!feof(p2))
- {
- if (fread(&stu[i], sizeof(struct Student), 1, p2) != -1)
- {
- if (j == stu[i].num)
- {
- printf("该学生的姓名 : %s \n", stu[i].name);
- printf("该学生的高数成绩: %.1lf \n", stu[i].score[0]);
- printf("该学生的英语成绩: %.1lf \n", stu[i].score[1]);
- printf("该学生的C++成绩 : %.1lf \n", stu[i].score[2]);
- }
- else
- {
- b++;
- }
- }
- }
- if (b == SIZE)
- {
- printf("该同学记录不存在!!\n");
- }
- i = t;
- printf("\n");
- fclose(p2);
- break;
- case 3:
- printf("请输入要修改学生的学号:");
- scanf("%d", &j);
- t = i;
- for (i = 0; i<SIZE; i++)
- {
- if ([color=Red]fread(&stu[i], sizeof(struct Student), 1, p3)[/color] != -1)
- {
- if (stu[i].num == j)
- {
- printf("该学生姓名: %s \n", stu[i].name);
- printf("请重新输入该学生的高数成绩: ");
- scanf("%lf", &stu[i].score[0]);
- printf("请重新输入该学生的英语成绩: ");
- scanf("%lf", &stu[i].score[1]);
- printf("请重新输入该学生的C++成绩 : ");
- scanf("%lf", &stu[i].score[2]);
- }
- }
- }
- if (([color=Red]p3 = fopen("stud", "rb")[/color]) == NULL)
- {
- printf("文件无法打开!\n");
- exit(0);
- }
- if (fwrite(&stu[i], sizeof(struct Student), 1, p3) != 1)
- {
- printf("输入错误!\n");
- }
- i = t;
- printf("\n");
- //fclose(p3);
- break;
- case 4:
- if ((p4 = fopen("stud", "r")) == NULL)
- {
- printf("文件无法打开!\n");
- exit(0);
- }
- t = i;
- for (i = 0; i<2; i++)
- {
- if (fread(&stu[i], sizeof(struct Student), 1, p4) != -1)
- {
- printf("学生姓名 : %s \n", stu[i].name);
- printf("学生的学号: %d \n", stu[i].num);
- printf("高数成绩为: %.1lf \n", stu[i].score[0]);
- printf("英语成绩为: %.1lf \n", stu[i].score[1]);
- printf("C++成绩为 : %.1lf \n", stu[i].score[2]);
- printf("\n");
- }
- }
- i = t;
- //fclose(p4);
- break;
- default:printf("输入错误\n");
- }
- printf("\n");
- printf("***请选择***\n");
- printf("1***返回主菜单\n");
- printf("0***退出程序\n");
- scanf("%d", &a);
- while (a != 1 && a != 0)
- {
- printf("输入错误,请重新输入:");
- scanf("%d", &a);
- }
- } while (a == 1);
- //fclose(p2);
- //fclose(p3);
- //fclose(p4);
- return 0;
- }
复制代码
没有仔细看 但发现 p3 在打开前使用了 |
|