c语言练习7

来源:互联网 发布:京东和淘宝的swot分析 编辑:程序博客网 时间:2024/05/16 17:08

7、有5个学生,每个学生有3门课程的成绩,从键盘输入学生数据(包括学号、姓名、3门课程成绩),将数据存放在磁盘文件“stud”中。

 

程序:

#include<stdio.h>
#define SIZE 5
struct STU
{
       int num;
       char name[20];
       float score[3];
}student[SIZE];
void stud()
{
     FILE *fp;
     int i,j;
     if((fp=fopen("stud.txt","w"))==NULL)
       {
           printf("cannot open file/n");
           return;                          
       }
       for(i=0;i<SIZE;i++)
       {
            fprintf(fp,"%-4d%-10s",student[i].num,student[i].name);
            for(j=0;j<3;j++)
            fprintf(fp,"%-8.2f",student[i].score[j]);
            fprintf(fp,"/n");
       }
       fclose(fp);                              
}
void main()
{
      int i,j;
      for(i=0;i<SIZE;i++)
      {              
                      scanf("%d%s",&student[i].num,student[i].name);
                      for(j=0;j<3;j++)
                      scanf("%f",&student[i].score[j]);
      }
      stud();
}