谢谢大家了 虽然问题没解决 我还是重新看好基础概念再来吧。
没病的人说有病的人有病,有病的人说没病的人有病。到底是谁有病?
#include <stdio.h> #include <string.h> #include <conio.h> typedef struct _student { char clas[20]; /*班级*/ int number; /*学号*/ char name[20]; /*姓名*/ float clan; /*C语言成绩*/ float english; /*大学英语成绩*/ float math; /*高等数学成绩*/ float sum; /*总分*/ float adver; /*平均分*/ } Student; const char fileName[] = "Students.DAT"; bool SaveData(Student* stu); bool LoadData(Student* stu, const int maxNumber); void ShowData(Student* stu); void main(void) { Student stu[50] = { { "中文10(1)", 1, "张三丰", 80.5, 70.0, 92.5 }, { "中文10(1)", 2, "丘处机", 77.0, 60.0, 95.0 }, { NULL } }; SaveData(stu); strcpy(stu[0].name, "梅超风"); LoadData(&stu[2], 1); ShowData(stu); _getch(); } bool SaveData(Student* stu) { FILE* file; if ((file = fopen(fileName, "wb")) == NULL) { return false; } while (*stu->clas) { fwrite((void *)stu, sizeof(*stu), 1, file); ++stu; } fclose(file); return true; } bool LoadData(Student* stu, const int maxNumber) { FILE* file; int counter = 0; if ((file = fopen(fileName, "rb")) == NULL) { return false; } while (!feof(file) && (counter++ < maxNumber)) { fread(stu, sizeof(*stu), 1, file); ++stu; } fclose(file); *stu->clas = NULL; return true; } void ShowData(Student* stu) { while (*stu->clas) { printf("班级: %-20s\n", stu->clas); printf("学号: %04d\n", stu->number); printf("姓名: %-20s\n", stu->name); printf("C语言: %6.2f\n", stu->clan); printf("英语: %6.2f\n", stu->english); printf("高数: %6.2f\n", stu->math); printf("\n"); ++stu; } }