读取文件时程序报错,调试了好久不知道如何解决
程序代码:
#ifndef STUDENT_SYSTEM_H #define STUDENT_SYSTEM_H void Studentmassage_ShowMenu(); void AddItem(); void Display(); void Sort(); void Find(); void RemoveItem(); void t(); void ModifyItem(); struct Student { char name[20]; char Id[20]; char sex[4]; int age; int Cnum; int Mnum; int Enum; int math; int sum; int average; }; typedef struct load { char user_name[50]; char password[50]; }mima; void build_new(); int load_user(); int load_mima(); void write_file(struct Student *s); void read_file(void); #endif
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "student_system.h" void write_file(struct Student *s) { FILE *fp_1; int mark=1; if((fp_1=fopen("list_student.txt","ab+"))==NULL) printf("打开文件失败"); else while(s->name[0]!=NULL) { fwrite(&mark,sizeof(int),1,fp_1); fwrite(s,sizeof(Student),1,fp_1); printf("存储数据成功"); system("pause"); s++; } fclose(fp_1); } void read_file() { FILE *fp_2; int mark; struct Student *s; s=(Student *)malloc(sizeof Student); if((fp_2=fopen("list_student.txt","rb"))==NULL) printf("打开文件失败"); else while(fread(&mark,sizeof(int),1,fp_2)==1) { fflush(stdout); fread(s,sizeof(Student),1,fp_2); printf("读取数据成功\n"); printf("%6s%6s%6s%6s%6s%6s%6s%6s%6s%6s\n","姓名","学号","性别","年龄","成绩1","成绩2","成绩3","成绩4","总成绩","平均成绩"); printf("%6s%6s%6s%6d%6d%6d%6d%6d%6d%6d\n",s->name,s->Id,s->sex,s->age,s->Cnum,s->Enum,s->Mnum,s->math,s->sum,s->average); s++; } /*system("pause");*/ free(s); fclose(fp_2); }
只附上相关的代码