溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C語言如何讀取文件求某一列的平均值

發(fā)布時(shí)間:2022-02-15 13:37:34 來源:億速云 閱讀:212 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“C語言如何讀取文件求某一列的平均值”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C語言如何讀取文件求某一列的平均值”吧!

第一部分:比較讀取文件的效率

首先隨機(jī)生成一個(gè)文件,1000萬行,4列(該文件下面還會用到)。我們看一下上述函數(shù)讀取文件的效率:

C語言如何讀取文件求某一列的平均值

從上圖中可以看出,fread 的效率最高,fgetc 的效率最低。當(dāng)然這種比較很粗淺,但是能大概看出趨勢。

各個(gè)函數(shù)讀取文件的代碼如下:其中 main 函數(shù)是一樣的,只是 readFile 函數(shù)的實(shí)現(xiàn)不同。

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define BUFSIZE 4096
    
    void readFile(FILE* fp);
    
    int main(int argc, char* argv[]) {
      FILE *fp;
      time_t start, end;
      start = time(NULL);
      if (argc < 2) {
        printf("Usage: %s <filename>\n", argv[0]);
        return 1;
      }
      if ((fp = fopen(argv[1], "r")) == NULL) {
        printf("Error: cannot open file\n");
        return 1;
      }
      readFile(fp);
      fclose(fp);
      end = time(NULL);
      printf("time spent: %d seconds\n", end - start);
      return 0;
    }
    // readFile_fgetc:
    void readFile(FILE* fp) {
      char c;
      while ((c = fgetc(fp)) != EOF)
        ;
    }
    // readFile_fgets:
    void readFile(FILE* fp) {
      char buf[BUFSIZE];
      while (fgets(buf, MAXLINE, fp) != NULL)
        ;
    }
    // readFile_fread:
    void readFile(FILE* fp) {
      char buf[BUFSIZE];
      while (fread(buf, 1, BUFSIZE, fp) > 0)
        ;
    }
    // readFile_fscanf:
    void readFile(FILE* fp) {
      char buf[BUFSIZE];
      while (fscanf(fp, " %[^\n]s", buf) == 1)
        ;
    }

第二部分:比較求取列平均值的效率

那么各個(gè)函數(shù)計(jì)算列平均值的效率如何呢?我們依然使用上面那1000萬行的文件,用上述各個(gè)函數(shù)實(shí)現(xiàn)計(jì)算第2列平均數(shù)的功能,它們的效率如下:

C語言如何讀取文件求某一列的平均值


代碼如下:main 函數(shù)大體上是一樣的,只是 colAver 函數(shù)的實(shí)現(xiàn)不一樣。
(這些代碼完善地處理了EOF,無論文件最后是否有空白行都可以正確運(yùn)行。但是仍然有前提,就是文件中每一行的分隔符(列數(shù))是一樣的,否則代碼可能會出錯(cuò)。)
這些代碼中,fscanf 的最簡短,該函數(shù)可以大大提高格式化讀取數(shù)據(jù)的編程效率。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BUFSIZE 4096
void getColAver(FILE* fp, const int k);
 int main(int argc, char* argv[]) {
   FILE *fp;
   time_t start, end;
   start = time(NULL);
   if (argc < 2) {
     printf("Usage: %s <filename>\n", argv[0]);
     return 1;
   }
   if ((fp = fopen(argv[1], "r")) == NULL) {
     printf("Error: cannot open file\n");
     return 1;
   }
   getColAver(fp, 2);
   fclose(fp);
   end = time(NULL);
   printf("time spent: %d seconds\n", end - start);
   return 0;
 }
 // colAver_fgetc:
 void getColAver(FILE* fp, const int k) {
   int i = 0;  // num of '\t'
   int j = 0;  // num of chars
   int c;  // char
   char col[50];
   float sum = 0;
   int n = 0;  // num of lines.
   int inCol = 0;
   while ((c = fgetc(fp)) != EOF) {
     if (i == k - 1) {
       inCol = 1;
       if (c == '\t') i++;
       else if (c == '\n') i = 0;
       else col[j++] = c;
     } else {
       if (c == '\t') i++;
       else if (c == '\n') i = 0;
       if (inCol) {
         col[j] = '\0';
         sum += atof(col);
         n++;
       }
       j = 0;
       inCol = 0;
     }
   }
   if (inCol) {
     col[j] = '\0';
     sum += atof(col);
     n++;
   }
   if (n == 0) printf("Error: no line!\n");
   else printf("The average of col %d is %f\n", k, sum / n);
 }
 // colAver_fgets:
 void getColAver(FILE* fp, const int k) {
   int i = 0;  // num of '\t'
   int j = 0;  // num of chars
   char col[50];
   char buf[BUFSIZE];
   float sum = 0;
   int n = 0;  // num of lines.
   int inCol = 0;
   char* p;
   while (fgets(buf, BUFSIZE, fp) != NULL) {
     for (p = buf; *p != '\0'; p++) {
       if (i == k - 1) {
         inCol = 1;
         if (*p == '\t') i++;
         else if (*p == '\n') i = 0;
         else col[j++] = *p;
       } else {
         if (*p == '\t') i++;
         else if (*p == '\n') i = 0;
         if (inCol) {
           col[j] = '\0';
           sum += atof(col);
           n++;
         }
         j = 0;
         inCol = 0;
       }
     }
   }
   if (inCol) {
     col[j] = '\0';
     sum += atof(col);
     n++;
   }
   if (n == 0) printf("Error: no line!\n");
   else printf("The average of col %d is %f\n", k, sum / n);
 }
 // colAver_fread:
 void getColAver(FILE* fp, const int k) {
   int i = 0;  // num of '\t'
   int j = 0;  // num of chars
   char col[50];
   char buf[BUFSIZE];
   float sum = 0;
   int n = 0;  // num of lines.
   int m, l;
   int sizeChr = sizeof(char);
   int inCol = 0;
   while ((l = fread(buf, sizeChr, BUFSIZE, fp)) > 0) {
     for (m = 0; m < l; m++) {
       if (i == k - 1) {
         inCol = 1;
         if (buf[m] == '\t') i++;
         else if (buf[m] == '\n') i = 0;
         else col[j++] = buf[m];
       } else {
         if (buf[m] == '\t') i++;
         else if (buf[m] == '\n') i = 0;
         if (inCol) {
           col[j] = '\0';
           sum += atof(col);
           n++;
         }
         j = 0;
         inCol = 0;
       }
     }
   }
   if (inCol) {
     col[j] = '\0';
     sum += atof(col);
     n++;
   }
   if (n == 0) printf("Error: no line!\n");
   else printf("The average of col %d is %f\n", k, sum / n);
 }
 // colAver_fscanf:
 void getColAver(FILE* fp) {
   float f;
   float sum = 0;
   int n = 0;  // num of lines.
   while (fscanf(fp, "%*s%f%*[^\n]s", &f) == 1) {
     sum += f;
     n++;
   }
   if (n == 0) printf("Error: no line!\n");
   else printf("The average of col 2 is %f\n", sum / n);
 }

到此,相信大家對“C語言如何讀取文件求某一列的平均值”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI