69,369
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <memory.h>
#include<stdlib.h>
struct student
{
int num;
char name[10];
float score1;
float score2;
float score3;
float aver;
};
main()
{
int i,nu;
float sc1,sc2,sc3;
char na[10];
struct student st[1];
FILE *fp;
if((fp=fopen("E:\student.txt","w+"))==NULL)
{
printf("can not open file\n");
exit(0);
}
setbuf(fp,NULL);
for(i=0; i<1; i++)
{
scanf("%d%s%f%f%f",&nu,&na,&sc1,&sc2,&sc3);
fprintf(fp,"%d %s %f %f %f %f",nu,na,sc1,sc2,sc3,(sc1,sc2,sc3)/3);
fflush(fp);
}
fseek(fp,0L,SEEK_SET); // 定位文件读写指针fp到文件开头
for(i=0; i<1; i++)
{
fscanf(fp,"%d%s%f%f%f%f%f",&st[i].num,&st[i].name,&st[i].score1,&st[i].score2,&st[i].score3,&st[i].aver);
}
for(i=0; i<1; i++)
{
printf("%d %s %.2f %.2f %.2f %.2f",st[i].num,st[i].name,st[i].score1,st[i].score2,st[i].score3,st[i].aver);
}
fclose(fp);
}