这也太快了吧!!!c处理csv文件(求和,平均值,最大值,最小值)3W多行秒处理!
本帖最后由 Cool_Breeze 于 2020-2-26 16:29 编辑命令行用法:D:\GIN\c\test>csv分析.exe source.csv result.txt 1//编译器 Dev-C++5.11
//Rev 02
//Author By Gin
//增加打印格式
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argv,char *argc[])
{
int delims(char *ser,char *res,int res_size);
FILE *file_p(char *path,const char *mode);
void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off);
int check_width(FILE *fp);
int check_high(FILE *fp);
FILE *source_txt=file_p(argc,"r"); //源文件
FILE *result_txt=file_p(argc,"w"); //结果
int off=atoi(argc); //输出结果格式
int width=check_width(source_txt); //文件列数统计
int high=check_high(source_txt); //文件行数统计
int Max=10240,T_max=50;
char temp; //用于临时储存一行数据 (用于atof转换)
char item={"0"}; //储存数据项目名
char line_size="0"; //储存一行数据空间
double sum={0}; //储存一行数据
int nu_line=0; //用来储存文件一行数据个数
rewind(source_txt);
fgets(line_size,Max,source_txt);
delims(line_size,item,T_max); //将第一行的标题单独存放
int i=0,k=0,j=0,count=0;
double min={0},max_1={0}; //最大值,最小值
while (fgets(line_size,Max,source_txt)!=NULL)
{
count++;
nu_line=delims(line_size,temp,T_max);
for (i=1;*(temp)!='\0';i++) //(i=1)跳过第一列的数据
{
if (i<nu_line) sum+=atof(temp); //将每一项数据相加
if (count==1)
{
min=atof(temp); //最少值(初始化)
max_1=atof(temp); //最大值(初始化)
}
else
{
if (atof(temp) > max_1) max_1=atof(temp);//最大值
if (atof(temp) < min) min=atof(temp); //最小值
}
}
}
double avg={0}; //平均值
for (i=0;i<width-1;i++)
{
if (i<nu_line-1) avg=sum/count; //最后一行如果小于width
else
{
avg=sum/(count-1);
}
}
form_printf(result_txt,item,sum,min,max_1,avg,width,T_max,off);
fclose(source_txt);
fclose(result_txt);
return 0;
}
int check_width(FILE *fp)
{
rewind(fp);
char t;
int w=1;
for (;(t=fgetc(fp))!='\n';)
{
if (t==',') w++;
}
return w;
}
int check_high(FILE *fp)
{
rewind(fp);
int h=0,size=10240;
char temp="0";
for (;fgets(temp,size,fp)!=NULL;h++);
return h-1; //去掉第一行
}
//分割数据
int delims(char *sou,char *res,int res_size)
{
int j=0,k=0,i=0,n=0;
for (;sou!='\0';i++)
{
if (sou!=',' && sou!=' ')
{
*(res+j*res_size+k)=sou;
k++;
}
if (sou==',')
{
*(res+j*res_size+k)='\0';
j++,n++,k=0;
}
if (sou=='\0') *(res+j*res_size+k-1)='\0';//把最后一个符号替换
}
return n+1; //加上最后一行
}
FILE *file_p(char *path,const char *mode)
{
FILE *fp;
if ((fp=fopen(path,mode))==NULL)
{
printf("%s open faile!",path);
exit(0);
}
return fp;
}
void form_printf(FILE *fp,char *item,double *sum,double *min,double *max_1,double *avg,int width,int T_max,int off)
{
int i=0;
if (off==1)
{
char form='+';
char form_1='|';
char form_2="---------------------" ;
char form_3="-------------------------------" ;
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);
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);
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);
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,form_1,min,form_1,max_1,form_1,avg,form_1);
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);
}
else if (off==2)
{
char form='+';
char form_1='|';
char form_2="---------------------" ;
char form_3="-------------------------------" ;
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);
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);
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,form_1,min,form_1,max_1,form_1,avg,form_1);
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);
}
else
{
fprintf(fp,"%-30s%-20s%-20s%-20s%-20s\n","Item","Sum","Min","Max","Avg");
for (i=0;i<width-1;i++) fprintf(fp,"%-30s%-20f%-20f%-20f%-20f\n",item+i*T_max+T_max,sum,min,max_1,avg);
}
} 格式【0】Item Sum Min Max Avg
VDDCR_GFXCurrent(A)(A) 4502714.966540 2.007840 244.956880 123.755359
GPUTemperature(C)(C) 2933954.125140 36.745070 84.673640 80.638581
JunctionTemperature(C)(C)3387563.903270 36.784480 99.827960 93.105868
MemTemperature(C)(C) 3384580.249270 34.000000 98.000000 93.023863
VR_GFX(C)(C) 2967047.724460 27.000000 85.000000 81.548145
VR_SOC(C)(C) 2790556.417030 29.000000 79.000000 76.697351
VR_MEM(C)(C) 0.000000 0.000000 0.000000 0.000000
VR_VDDCI(C)(C) 0.000000 0.000000 0.000000 0.000000
Liquid0(C)(C) 0.000000 0.000000 0.000000 0.000000
Liquid1(C)(C) 0.000000 0.000000 0.000000 0.000000
PLX(C)(C) 0.000000 0.000000 0.000000 0.000000
Min(C)(C) 2755640.988070 35.354680 79.158970 75.737714
GFXCLKFreq() 69755491.926140 804.097780 2067.620360 1917.202395
PWM() 1782709.728940 0.000000 60.548110 48.997079
FANSpeed() 78159803.685400 0.000000 2452.709470 2148.191614
LimitPPT0(W)(W) 7094880.000000 195.000000 195.000000 195.000000
ValuePPT0(W)(W) 6370334.954680 10.208000 195.003540 175.086163
GFXActivity(%)(%) 3120834.797830 0.073530 99.975200 85.774923
PCIeLinkSpeed(GT/s)(GT/s)289350.500000 2.500000 8.000000 7.952685
PCIeLinkWidth() 582144.000000 16.000000 16.000000 16.000000
PCIeCorrectableError() 0.000000 0.000000 0.000000 0.000000
PCIeUncorrectableError() 0.000000 0.000000 0.000000 0.000000
PCIeResidencyGen1(%)(%) 114556.277570 2.522750 51.898740 3.148534
PCIeResidencyGen2(%)(%) 0.000000 0.000000 0.000000 0.000000
PCIeResidencyGen3(%)(%) 3523843.723860 48.101270 97.477250 96.851466
PCIeResidencyGen4(%)(%) 0.000000 0.000000 0.000000 0.000000
PCIeResidencyL0(%)(%) 3638374.158400 99.997400 100.000000 99.999290
PCIeResidencyL0s(%)(%) 0.000000 0.000000 0.000000 0.000000
PCIeResidencyL1(%)(%) 0.000000 0.000000 0.000000 0.000000
FanPWMreading[%](%) 1782582.000000 0.000000 60.000000 48.993569
mclk(MHz) 62883779.700000 101.000000 32327430.000000 1728.383577
sclk(MHz) 69639761.000000 796.000000 2084.000000 1914.074183 本帖最后由 Cool_Breeze 于 2020-2-26 16:30 编辑
格式【1】+-------------------------------+---------------------+---------------------+---------------------+---------------------+
| Item | Sum | Min | Max | Avg |
+-------------------------------+---------------------+---------------------+---------------------+---------------------+
| VDDCR_GFXCurrent(A)(A) | 4502714.966540 | 2.007840 | 244.956880 | 123.755359 |
| GPUTemperature(C)(C) | 2933954.125140 | 36.745070 | 84.673640 | 80.638581 |
| JunctionTemperature(C)(C)| 3387563.903270 | 36.784480 | 99.827960 | 93.105868 |
| MemTemperature(C)(C) | 3384580.249270 | 34.000000 | 98.000000 | 93.023863 |
| VR_GFX(C)(C) | 2967047.724460 | 27.000000 | 85.000000 | 81.548145 |
| VR_SOC(C)(C) | 2790556.417030 | 29.000000 | 79.000000 | 76.697351 |
| VR_MEM(C)(C) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| VR_VDDCI(C)(C) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| Liquid0(C)(C) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| Liquid1(C)(C) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PLX(C)(C) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| Min(C)(C) | 2755640.988070 | 35.354680 | 79.158970 | 75.737714 |
| GFXCLKFreq() | 69755491.926140 | 804.097780 | 2067.620360 | 1917.202395 |
| PWM() | 1782709.728940 | 0.000000 | 60.548110 | 48.997079 |
| FANSpeed() | 78159803.685400 | 0.000000 | 2452.709470 | 2148.191614 |
| LimitPPT0(W)(W) | 7094880.000000 | 195.000000 | 195.000000 | 195.000000 |
| ValuePPT0(W)(W) | 6370334.954680 | 10.208000 | 195.003540 | 175.086163 |
| GFXActivity(%)(%) | 3120834.797830 | 0.073530 | 99.975200 | 85.774923 |
| PCIeLinkSpeed(GT/s)(GT/s)| 289350.500000 | 2.500000 | 8.000000 | 7.952685 |
| PCIeLinkWidth() | 582144.000000 | 16.000000 | 16.000000 | 16.000000 |
| PCIeCorrectableError() | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PCIeUncorrectableError() | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PCIeResidencyGen1(%)(%) | 114556.277570 | 2.522750 | 51.898740 | 3.148534 |
| PCIeResidencyGen2(%)(%) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PCIeResidencyGen3(%)(%) | 3523843.723860 | 48.101270 | 97.477250 | 96.851466 |
| PCIeResidencyGen4(%)(%) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PCIeResidencyL0(%)(%) | 3638374.158400 | 99.997400 | 100.000000 | 99.999290 |
| PCIeResidencyL0s(%)(%) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| PCIeResidencyL1(%)(%) | 0.000000 | 0.000000 | 0.000000 | 0.000000 |
| FanPWMreading[%](%) | 1782582.000000 | 0.000000 | 60.000000 | 48.993569 |
| mclk(MHz) | 62883779.700000 | 101.000000 | 32327430.000000 | 1728.383577 |
| sclk(MHz) | 69639761.000000 | 796.000000 | 2084.000000 | 1914.074183 |
+-------------------------------+---------------------+---------------------+---------------------+---------------------+ 源文件【前13行】time stamp, VDDCR_GFX Current (A) (A), GPU Temperature (C) (C), Junction Temperature (C) (C), Mem Temperature (C) (C), VR_GFX (C) (C), VR_SOC (C) (C), VR_MEM (C) (C), VR_VDDCI (C) (C), Liquid0 (C) (C), Liquid1 (C) (C), PLX (C) (C), Min (C) (C), GFXCLK Freq (), PWM (), FAN Speed (), Limit PPT0 (W) (W), Value PPT0 (W) (W), GFX Activity (%) (%), PCIe Link Speed (GT/s) (GT/s), PCIe Link Width (), PCIe Correctable Error (), PCIe Uncorrectable Error (), PCIe Residency Gen 1 (%) (%), PCIe Residency Gen 2 (%) (%), PCIe Residency Gen 3 (%) (%), PCIe Residency Gen 4 (%) (%), PCIe Residency L0 (%) (%), PCIe Residency L0s (%) (%), PCIe Residency L1 (%) (%), Fan PWM reading [%] (%), mclk (MHz), sclk (MHz)
19:29:01.189, 2.00784, 36.74507, 36.78448, 34.23645, 27.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.35468, 840.95294, 0.00000, 0.00000, 195.00000, 10.20800, 0.67411, 8.00000, 16.00000, 0.00000, 0.00000, 50.00000, 0.00000, 50.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,799.0
19:29:02.245, 2.00784, 37.07646, 37.30193, 34.00000, 27.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.63988, 1083.11572, 0.00000, 0.00000, 195.00000, 15.59418, 8.34534, 2.50000, 16.00000, 0.00000, 0.00000, 41.66666, 0.00000, 58.33333, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,798.0
19:29:03.328, 2.00784, 36.84468, 36.91053, 34.00000, 27.13817, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.55442, 859.78790, 0.00000, 0.00000, 195.00000, 11.25746, 1.79272, 8.00000, 16.00000, 0.00000, 0.00000, 42.85714, 0.00000, 57.14286, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,799.0
19:29:04.375, 2.00784, 36.95381, 36.97800, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.45821, 843.08490, 0.00000, 0.00000, 195.00000, 11.40743, 1.24570, 2.50000, 16.00000, 0.00000, 0.00000, 38.70967, 0.00000, 61.29033, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,798.0
19:29:05.435, 6.02353, 36.79740, 36.84480, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.48343, 877.18518, 0.00000, 0.00000, 195.00000, 11.50216, 1.88790, 8.00000, 16.00000, 0.00000, 0.00000, 42.50000, 0.00000, 57.50000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,1856.0
19:29:06.510, 2.00784, 36.96594, 37.03910, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.58804, 884.69458, 0.00000, 0.00000, 195.00000, 12.11919, 2.64405, 8.00000, 16.00000, 0.00000, 0.00000, 50.00000, 0.00000, 50.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 501.0,1463.0
19:29:07.610, 2.00784, 36.99527, 37.04279, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.63160, 902.23077, 0.00000, 0.00000, 195.00000, 11.99589, 2.81013, 2.50000, 16.00000, 0.00000, 0.00000, 50.00000, 0.00000, 50.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,798.0
19:29:08.710, 2.00784, 37.03320, 37.09886, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.61695, 900.25281, 0.00000, 0.00000, 195.00000, 12.10599, 2.83194, 2.50000, 16.00000, 0.00000, 0.00000, 49.27536, 0.00000, 50.72464, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,798.0
19:29:09.790, 2.00784, 37.07740, 37.11398, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.70216, 832.81805, 0.00000, 0.00000, 195.00000, 11.13766, 1.00379, 8.00000, 16.00000, 0.00000, 0.00000, 51.89874, 0.00000, 48.10127, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,800.0
19:29:10.870, 12.04706, 37.09166, 37.14910, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.71678, 849.42194, 0.00000, 0.00000, 195.00000, 11.02654, 1.50605, 2.50000, 16.00000, 0.00000, 0.00000, 51.13636, 0.00000, 48.86364, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,1045.0
19:29:11.970, 4.01569, 37.51789, 37.59493, 34.00000, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 35.91874, 996.54883, 0.00000, 0.00000, 195.00000, 15.02523, 4.10683, 8.00000, 16.00000, 0.00000, 0.00000, 47.95918, 0.00000, 52.04082, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,1652.0
19:29:13.080, 4.01569, 38.43451, 39.28155, 34.77629, 28.00000, 29.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 36.40010, 1224.01880, 0.00000, 0.00000, 195.00000, 31.80527, 16.07076, 8.00000, 16.00000, 0.00000, 0.00000, 44.44445, 0.00000, 55.55556, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 876.0,799.0
页:
[1]