我将平均值存在学生信息后面的 #include#include struct student{ char name[10]; char number[10]; float English_Score;
float Math_Score; float C_Score; float Sport_Score; float Average;}stu[10];int main(){ int i; FILE *fp;
for(i=0;i<10;i++) { //输入数据 scanf("%s%s%f%f%f%f",stu.name,stu.number,&stu.English_Score,
&stu.Math_Score,&stu.C_Score,&stu.Sport_Score);
//求平均值 stu.Average=(stu.English_Score+stu.Math_Score+
stu.C_Score+stu.Sport_Score)/4;} printf("\n"); //输出数据
for(i=0;i<10;i++) {
printf("%-8s%-10s%-5.1f%-5.1f%-5.1f%-5.1f%-5.1f\n",stu.name,
stu.number,stu.English_Score,stu.Math_Score,
stu.C_Score,stu.Sport_Score,stu.Average); }
//写入文件
if((fp=fopen("xinxi","w"))==NULL) {
printf("cannot open file!\n");
exit(0); }
for(i=0;i<10;i++) {
fwrite(&stu,sizeof(struct student),1,fp); }
getchar();getchar(); fclose(fp);
return 0;}//另附读入数据模块(要把上个程序得到的xinxi文件拷到当前工作区下) if((fp=fopen("xinxi","r"))==NULL) { printf("cannot open file!\n");exit(0); }
for(i=0;i<10;i++) {fread(&stu,sizeof(struct student),1,fp); }
|