找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 15987|回复: 4

这也太快了吧!!!c处理csv文件(求和,平均值,最大值,最小值)3W多行秒处理!

[复制链接]
发表于 2020-2-20 20:35:15 | 显示全部楼层 |阅读模式
本帖最后由 Gin_Q 于 2020-2-27 10:09 编辑

命令行用法:D:\GIN\c\test>csv分析.exe source.csv result.txt 1
  1. //编译器 Dev-C++  5.11
  2. //Rev 02
  3. //Author By Gin
  4. //增加打印格式
  5. //.cpp

  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>



  9. int main(int argv,char *argc[])
  10. {       
  11.         int delims(char *ser,char *res,int res_size);
  12.         FILE *file_p(char *path,const char *mode);
  13.         void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off);
  14.         int check_width(FILE *fp);
  15.         int check_high(FILE *fp);

  16.         FILE *source_txt=file_p(argc[1],"r");                        //源文件
  17.         FILE *result_txt=file_p(argc[2],"w");                        //结果

  18.         int off=atoi(argc[3]);                        //输出结果格式
  19.         int width=check_width(source_txt);        //文件列数统计
  20.         int high=check_high(source_txt);        //文件行数统计
  21.         int Max=10240,T_max=50;
  22.         char temp[width][T_max];                        //用于临时储存一行数据 (用于atof转换)
  23.         char item[width][T_max]={"0"};                        //储存数据项目名
  24.         char line_size[Max]="0";                        //储存一行数据空间
  25.         double sum[width]={0};                        //储存一行数据
  26.         int nu_line=0;                        //用来储存文件一行数据个数
  27.        
  28.         rewind(source_txt);
  29.         fgets(line_size,Max,source_txt);
  30.         delims(line_size,item[0],T_max);                     //将第一行的标题单独存放
  31.        
  32.         int i=0,k=0,j=0,count=0;
  33.         double min[width]={0},max_1[width]={0};   //最大值,最小值
  34.         while (fgets(line_size,Max,source_txt)!=NULL)
  35.         {
  36.                 count++;
  37.                 nu_line=delims(line_size,temp[0],T_max);
  38.                 for (i=1;*(temp[i])!='\0';i++)    //(i=1)跳过第一列的数据
  39.                 {
  40.                         if (i<nu_line) sum[i-1]+=atof(temp[i]);    //将每一项数据相加        
  41.                         if (count==1)
  42.                         {
  43.                                 min[i-1]=atof(temp[i]);         //最少值(初始化)       
  44.                                 max_1[i-1]=atof(temp[i]);   //最大值(初始化)       
  45.                         }
  46.                         else
  47.                         {
  48.                                 if (atof(temp[i]) > max_1[i-1]) max_1[i-1]=atof(temp[i]);  //最大值
  49.                                 if (atof(temp[i]) < min[i-1]) min[i-1]=atof(temp[i]);      //最小值
  50.                         }
  51.                 }
  52.         }
  53.         double avg[width]={0};      //平均值
  54.         for (i=0;i<width-1;i++)
  55.         {
  56.                 if (i<nu_line-1) avg[i]=sum[i]/count;   //最后一行如果小于width
  57.                 else
  58.                 {
  59.                         avg[i]=sum[i]/(count-1);
  60.                 }
  61.         }
  62.         form_printf(result_txt,item[0],sum,min,max_1,avg,width,T_max,off);
  63.         fclose(source_txt);
  64.         fclose(result_txt);
  65.         return 0;
  66. }
  67. int check_width(FILE *fp)
  68. {
  69.         rewind(fp);
  70.         char t;
  71.         int w=1;
  72.         for (;(t=fgetc(fp))!='\n';)
  73.         {
  74.                 if (t==',') w++;
  75.         }
  76.         return w;
  77. }
  78. int check_high(FILE *fp)
  79. {
  80.         rewind(fp);
  81.         int h=0,size=10240;
  82.         char temp[size]="0";
  83.         for (;fgets(temp,size,fp)!=NULL;h++);
  84.         return h-1;                        //去掉第一行
  85. }
  86. //分割数据
  87. int delims(char *sou,char *res,int res_size)
  88. {
  89.         int j=0,k=0,i=0,n=0;
  90.         for (;sou[i]!='\0';i++)
  91.         {
  92.                 if (sou[i]!=',' && sou[i]!=' ')
  93.                 {
  94.                         *(res+j*res_size+k)=sou[i];       
  95.                         k++;
  96.                 }
  97.                 if (sou[i]==',')
  98.                 {
  99.                         *(res+j*res_size+k)='\0';
  100.                         j++,n++,k=0;
  101.                 }
  102.                 if (sou[i+1]=='\0') *(res+j*res_size+k-1)='\0';  //把最后一个符号替换
  103.         }
  104.         return n+1;                        //加上最后一行
  105. }
  106. FILE *file_p(char *path,const char *mode)
  107. {
  108.         FILE *fp;
  109.         if ((fp=fopen(path,mode))==NULL)
  110.         {
  111.                 printf("%s open faile!",path);
  112.                 exit(0);
  113.         }
  114.         return fp;
  115. }
  116. void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off)
  117. {
  118.         int i=0;
  119.         if (off==1)
  120.         {
  121.                 char form='+';
  122.                 char form_1='|';
  123.                 char form_2[22]="---------------------" ;
  124.                 char form_3[32]="-------------------------------" ;
  125.                 fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
  126.                 fprintf(fp,"%c %-30s%c %-20s%c %-20s%c %-20s%c %-20s%c\n",form_1,"Item",form_1,"Sum",form_1,"Min",form_1,"Max",form_1,"Avg",form_1);
  127.                 fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
  128.                 for (i=0;i<width-1;i++) fprintf(fp,"%c %-30s%c %-20f%c %-20f%c %-20f%c %-20f%c\n",form_1,item+i*T_max+T_max,form_1,sum[i],form_1,min[i],form_1,max_1[i],form_1,avg[i],form_1);
  129.                 fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
  130.         }
  131.         else if (off==2)
  132.         {
  133.                 char form='+';
  134.                 char form_1='|';
  135.                 char form_2[22]="---------------------" ;
  136.                 char form_3[32]="-------------------------------" ;
  137.                 fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
  138.                 fprintf(fp,"%c %-30s%c %-20s%c %-20s%c %-20s%c %-20s%c\n",form_1,"Item",form_1,"Sum",form_1,"Min",form_1,"Max",form_1,"Avg",form_1);
  139.                 for (i=0;i<width-1;i++) fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n%c %-30s%c %-20f%c %-20f%c %-20f%c %-20f%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form,form_1,item+i*T_max+T_max,form_1,sum[i],form_1,min[i],form_1,max_1[i],form_1,avg[i],form_1);
  140.                 fprintf(fp,"%c%s%c%s%c%s%c%s%c%s%c\n",form,form_3,form,form_2,form,form_2,form,form_2,form,form_2,form);
  141.         }
  142.         else
  143.         {
  144.                 fprintf(fp,"%-30s%-20s%-20s%-20s%-20s\n","Item","Sum","Min","Max","Avg");
  145.                 for (i=0;i<width-1;i++) fprintf(fp,"%-30s%-20f%-20f%-20f%-20f\n",item+i*T_max+T_max,sum[i],min[i],max_1[i],avg[i]);
  146.         }
  147. }
复制代码
发表于 2020-2-20 22:51:42 | 显示全部楼层
回复 3# Gin_Q
最后一项用long long 就有精度了
发表于 2020-2-26 23:37:28 | 显示全部楼层
本帖最后由 red2020 于 2020-2-26 23:53 编辑

回复 10# Gin_Q
你们速度都太慢了,我只需要66行代码0.3秒就能处理3万行。在windows下用gcc编译下,甭管你cpu多弱,只需0.3秒3万行。最后一行自带计时器
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. #define LINE_SIZE 1024

  5. #define FLT_MIN (1.17549e-038)
  6. #define FLT_MAX (3.40282e+038)

  7. #define COLS_SIZE 32
  8. float a[COLS_SIZE][3];
  9. char iName[COLS_SIZE][LINE_SIZE];

  10. int main(int argc, char** argv)
  11. {
  12.         clock_t t = clock();
  13.         FILE* fp = fopen(argv[1], "rb");

  14.         for(int i = 0; i < COLS_SIZE; i ++) a[i][1] = FLT_MAX, a[i][2] = FLT_MIN;
  15.         int countLines =  0, colsIndex = 0, strIndex = 0;
  16.                         
  17.         char line[LINE_SIZE] = {0};               
  18.         fgets(line, LINE_SIZE, fp);
  19.         char* p = line;       
  20.         while(*p)
  21.         {
  22.                 switch(*p)
  23.                 {
  24.                 case ' ':
  25.                 case '\t':
  26.                 case '\r':
  27.                 case '\n':
  28.                         break;
  29.                 case ',':
  30.                         colsIndex ++;
  31.                         strIndex = 0;
  32.                         break;
  33.                 default:
  34.                         iName[colsIndex][strIndex++] = *p;
  35.                         break;
  36.                 }
  37.                 p ++;
  38.         }

  39.         colsIndex = 0;
  40.         while(! feof(fp))
  41.         {
  42.                 fgets(line, LINE_SIZE, fp);
  43.                 p = line;
  44.                 while(*p)
  45.                 if(*(p ++) == ',')
  46.                 {
  47.                         float t = atof(p);
  48.                         a[colsIndex][0] += t;
  49.                         if( t < a[colsIndex][1] ) a[colsIndex][1] = t;
  50.                         if( t > a[colsIndex][2] ) a[colsIndex][2] = t;
  51.                         if(++ colsIndex == 32) colsIndex = 0, countLines ++;
  52.                 }
  53.         }
  54.         fclose(fp);

  55.         printf("%-30.30s %16s %16s %16s %16s\n","Item","Sum","Min","Max","Avg");       
  56.         for(int i = 0; i < COLS_SIZE; i ++) printf("%-30.30s -> %16.6f %16.6f %16.6f %16.6f\n", iName[i+1], a[i][0], a[i][1], a[i][2], a[i][0]/countLines);
  57.         printf("Count %d lines spend time :%d (ms)\n", countLines, clock()-t);
  58.         return 0;
  59. }
复制代码
这是效果图,整齐的一塌糊涂
  1. Item                                        Sum              Min              Max              Avg
  2. VDDCR_GFXCurrent(A)[0](A)      ->    214949.328125         2.007840      4000.015625         3.582489
  3. GPUTemperature(C)[0](C)        ->   2227779.500000        36.745071        38.434509        37.129658
  4. JunctionTemperature(C)[0](C)   ->   2235515.250000        36.784481        39.281551        37.258587
  5. MemTemperature(C)[0](C)        ->   2045027.750000        34.000000        34.776291        34.083796
  6. VR_GFX(C)[0](C)                ->   1665636.750000        27.000000        28.000000        27.760613
  7. VR_SOC(C)[0](C)                ->   1740000.000000        29.000000        29.000000        29.000000
  8. VR_MEM(C)[0](C)                ->         0.000000         0.000000         0.000000         0.000000
  9. VR_VDDCI(C)[0](C)              ->         0.000000         0.000000         0.000000         0.000000
  10. Liquid0(C)[0](C)               ->         0.000000         0.000000         0.000000         0.000000
  11. Liquid1(C)[0](C)               ->         0.000000         0.000000         0.000000         0.000000
  12. PLX(C)[0](C)                   ->         0.000000         0.000000         0.000000         0.000000
  13. Min(C)[0](C)                   ->   2140390.000000        35.354679        36.400101        35.673167
  14. GFXCLKFreq[0]()                ->  55465844.000000       832.818054      1224.018799       924.430733
  15. PWM[0]()                       ->         0.000000         0.000000         0.000000         0.000000
  16. FANSpeed[RPM][0]()             ->         0.000000         0.000000         0.000000         0.000000
  17. LimitPPT0(W)[0](W)             ->  11700000.000000       195.000000       195.000000       195.000000
  18. ValuePPT0(W)[0](W)             ->    825928.562500        10.208000        31.805269        13.765476
  19. GFXActivity(%)[0](%)           ->    224570.906250         0.674110        16.070761         3.742848
  20. PCIeLinkSpeed(GT/s)[0](GT/s)   ->    342500.000000         2.500000         8.000000         5.708333
  21. PCIeLinkWidth[0]()             ->    960000.000000        16.000000        16.000000        16.000000
  22. PCIeCorrectableError[0]()      ->         0.000000         0.000000         0.000000         0.000000
  23. PCIeUncorrectableError[0]()    ->         0.000000         0.000000         0.000000         0.000000
  24. PCIeResidencyGen1(%)[0](%)     ->   2802648.250000        38.709671        51.898739        46.710804
  25. PCIeResidencyGen2(%)[0](%)     ->         0.000000         0.000000         0.000000         0.000000
  26. PCIeResidencyGen3(%)[0](%)     ->   3197214.750000        48.101269        61.290329        53.286913
  27. PCIeResidencyGen4(%)[0](%)     ->         0.000000         0.000000         0.000000         0.000000
  28. PCIeResidencyL0(%)[0](%)       ->   6000000.000000       100.000000       100.000000       100.000000
  29. PCIeResidencyL0s(%)[0](%)      ->         0.000000         0.000000         0.000000         0.000000
  30. PCIeResidencyL1(%)[0](%)       ->         0.000000         0.000000         0.000000         0.000000
  31. FanPWMreading[%][0](%)         ->         0.000000         0.000000         0.000000         0.000000
  32. mclk[0](MHz)                   ->  11934225.000000       101.000000       876.000000       198.903750
  33. sclk[0](MHz)                   ->  62049740.000000       798.000000      1856.000000      1034.162333
  34. Count 60000 lines spend time :667 (ms)
复制代码

评分

参与人数 1技术 +1 收起 理由
Gin_Q + 1 精辟!

查看全部评分

发表于 2020-2-27 10:45:29 | 显示全部楼层
回复 14# Gin_Q
学C语言要注重细节,化繁为简,代码要求精,必要的数学技巧也得学学。看得出你只是刚学会了语法,并没有掌握这门语言。
发表于 2020-2-27 21:04:56 | 显示全部楼层
回复 16# Gin_Q
所以说细节很重要,只要能认真观察,发现事物的客观规律,你就会找到编程的捷径,同样一个功能实现的方法有很多种,善于对比,发觉其中的技巧,更有助于C水平的提升。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 00:29 , Processed in 0.021989 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表