文件的读写,怎么出现结果后还报错
#include<stdio.h>#include<stdlib.h>
typedef struct Student
{
int id;
int age;
}ST,*PST;
void input(PST);
void output(PST);
void write(PST);
void read(PST);
main()
{
ST stu[2];
input(stu);
output(stu);
write(stu);
read(stu);
}
void input(PST stu)
{
int i;
for (i=0; i<2; i++)
scanf("%d%d",&stu[i].id,&stu[i].age);
}
void output(PST stu)
{
int i;
for (i=0; i<2; i++)
printf("%d %d\n",stu[i].id,stu[i].age);
}
void write(PST stu)
{
int i;
PST p = stu;
FILE *fp;
if ((fp = fopen("stu_list.txt","wb")) == NULL)
{
printf("write error");
return;
}
for (i=0; i<2;i++)
{
if (fwrite(p,sizeof(ST),1,fp) != 1)
{
printf("write error");
break;
}
p++;
}
fclose(fp);
}
void read(PST stu)
{
PST p = stu;
FILE *fp;
fp = fopen("stu_list.txt","rb");
while (!feof(fp))
{
if ( fread(p,sizeof(ST),1,fp) )
printf("%d %d\n",p->id,p->age);
}
fclose(fp);
}