欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          《C語言實驗指導》實驗九 文件

          實驗九 文件

          目的與要求】

          1.掌握文件和文件指針的概念以及文件的定義方法。

          2.了解文件打開和關閉的概念和方法。

          3.掌握有關文件的函數(shù)。

          【上機內(nèi)容】

          【一般示例】

          【例1】對data.dat文件寫入100條記錄。

          #include <stdio.h>

          main()

          {FILE *fp;

          int i;

          float x;

          fp=fopen(“date.dat”,”w”);

          for(i=1;i<=100;i++)

          {scanf(“%f”,&x);

          fprintf(fp,”%fn”,x);

          }

          fclose(fp);

          }

          【例2】設有一文件cj.dat存放了50個人的成績(英語、計算機、數(shù)學),存放格式為:每人一行,成績間由逗號分隔。計算三門課平均成績,統(tǒng)計個人平均成績大于或等于90分的學生人數(shù)。

          #include <stdio.h>

          main()

          {FILE *fp;

          int num;

          float x , y , z , s1 , s2 , s3 ;

          fp=fopen (“cj.dat”,”r”);

          {fscanf (fp,”%f,%f,%f”,&x,&y,&z);

          s1=s1+x;

          s2=s2+y;

          s3=s3+z;

          if((x+y+z)/3>=90)

          num=num+1;

          }

          printf(“分數(shù)高于90的人數(shù)為:%.2d”,num);

          fclose(fp);

          }

          【例3】統(tǒng)計上題cj.dat文件中每個學生的總成績,并將原有數(shù)據(jù)和計算出的總分數(shù)存放在磁盤文件“stud”中。

          #include “stdio.h”

          main()

          {

          FILE *fp1,*fp2;

          float x,y,z;

          fp1=fopen(“cj.dat”,”r”);

          fp2=fopen(“stud”,”w”);

          while(!feof(fp1))

          {

          fscanf (fp1,”%f,%f,%f”,&x,&y,&z);

          printf(“%f,%f,%f,%fn”,x,y,z,x+y+z);

          fprintf(fp2,”%f,%f,%f,%fn”,x,y,z,x+y+z);

          }

          fclose(fp1);

          fclose(fp2);

          }

          【作業(yè)】

          1. 有 5個學生,每個學生有3門課的成績,從鍵盤輸入以上數(shù)據(jù)(包括學生號,姓名,三門課成績),計算出平均成績,將原有數(shù)據(jù)和計算出的平均分數(shù)存放在磁盤文件“stud”中。

          2. 將上題“stud”文件中的學生數(shù)據(jù),按平均分進行排序處理,將已排序的學生數(shù)據(jù)存入一個新文件“stu_sort”中。

           

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號