#include<stdio.h>
#include<stdlib.h>
#define MAX 2
struct Stu{
char cno[10];
char name[20];
float score[3];
float aver;
}stu[MAX];
void save()
{
int i;
FILE *fp;
if((fp=fopen("stud.txt","wb"))==NULL)
{
printf("open error\n");
exit(0);
}
for(i=0;i<MAX;i++)
{
if(fwrite(&stu[i],sizeof(struct Stu),1,fp)!=1)
{
printf("write error\n");
}
fclose(fp);
}
}
int main()
{
int i,j,sum;
//struct Stu stu[MAX];
FILE *fp;
for(i=0;i<MAX;i++)
{
printf("请输入第%d个学生的信息:\n",i);
printf("学号:");
scanf("%s",stu[i].cno);
printf("姓名:");
scanf("%s",stu[i].name);
sum=0;
for(j=0;j<3;j++)
{
printf("第%d个成绩:",j);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].aver=sum/3.0;
}
save();
/*FILE *fp;
if((fp=fopen("stud","rb"))==NULL)
{
printf("cannot open\n");
exit(0);
}*/
return 0;
}