额,这里确实错了,忘记加括弧了
但是执行void read_file()还是报相同的错误
但是执行void read_file()还是报相同的错误
void read_file() { FILE *fp_2; int mark; struct Student *s; s=(Student *)malloc(sizeof(Student)*1000); int p=(int)s; //定义一个整形变量,让它保持s初始值 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((Student*)p); //这里实际上释放的是s分配的内存 fclose(fp_2); }