这也太快了吧!!!处理csv文档(求和,平均数,最大数,最小数)3W多行1秒就搞定了!
本帖最后由 Cool_Breeze 于 2020-2-21 09:08 编辑//编译器 Dev-C++5.11
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SUN_X 50 //存储数据大小
int max=10240;
char tempc={0};
char (*t_p)=tempc;
double dou_sum={0};//待处理数据个数
int nu=0;
void delims(char *ser,char res[],int n);//n=计数
FILE *file_p(char *path,const char *mode);
int main(int argv,char *argc[])
{
FILE *fp;
FILE *b_txt;
fp=file_p(argc,"r"); //源文件
b_txt=file_p(argc,"w"); //结果
char arr;
char name={0};
char (*p)=name;
fgets(arr,max,fp);
delims(arr,name,nu); //将第一行的标题单独存放
int i=0,k=0,j=0,count=0;
double min={0},max_1={0}; //最大值,最小值
while (fgets(arr,max,fp)!=NULL)
{
nu=0;
count++;
delims(arr,tempc,nu);
for (i=1;*(*(t_p+i)+0)!='\0';i++) //(i=1)跳过第一列的数据
{
if (i<=nu) dou_sum+=atof(*(t_p+i)); //将每一项数据相加
if (count==1)
{
min=atof(*(t_p+i)); //最少值(初始化)
max_1=atof(*(t_p+i)); //最大值(初始化)
}
else
{
if (atof(*(t_p+i)) > max_1) max_1=atof(*(t_p+i));//最大值
if (atof(*(t_p+i)) < min) min=atof(*(t_p+i)); //最小值
}
}
}
//printf("%d\t%d",nu,count);
double avg={0}; //平均值
for (i=0;*(*(p+i+1)+0)!='\0';i++)
{
if (i<nu) avg=dou_sum/count; //最后一行
else
{
avg=dou_sum/(count-1);
}
}
p=name;
//打印表格
/*
char b_ge="+------------------------------+" ;
char b_ge1="--------------------+" ;
fprintf(b_txt,"%s%s%s%s%s\n|%-30s|%-20s|%-20s|%-20s|%-20s|\n",b_ge,b_ge1,b_ge1,b_ge1,b_ge1,"Item","Sum","Min","Max","Avg");
for (i=0;*(*(p+i+1)+0)!='\0';i++) fprintf(b_txt,"%s%s%s%s%s\n|%-30s|%-20f|%-20f|%-20f|%-20f|\n",b_ge,b_ge1,b_ge1,b_ge1,b_ge1,*(p+i+1),dou_sum,min,max_1,avg);//(p+i+1)跳过第一列的数据
fprintf(b_txt,"%s%s%s%s%s",b_ge,b_ge1,b_ge1,b_ge1,b_ge1);
*/
fprintf(b_txt,"%-30s%-20s%-20s%-20s%-20s\n","Item","Sum","Min","Max","Avg");
for (i=0;*(*(p+i+1)+0)!='\0';i++) fprintf(b_txt,"%-30s%-20lf%-20f%-20f%-20lf\n",*(p+i+1),dou_sum,min,max_1,avg);//(p+i+1)跳过第一列的数据
fclose(fp);
fclose(b_txt);
return 0;
}
//分割每一项放到数组tempc
void delims(char *ser,char res[],int n)
{
int j=0,k=0,i=0;
for (;ser!='\0';i++)
{
if (ser!=',' && ser!=' ')
{
res=ser;
k++;
}
if (ser==',')
{
res='\0';
j++,nu++,k=0;
}
if (ser=='\0') res='\0';//把最后一个符号替换
}
}
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;
} 本帖最后由 Cool_Breeze 于 2020-3-15 18:44 编辑
结果如下:
Nu Item Max Min Sum Avg
1GPU Temperature (C) (C) 74.62657 40.57681 8499.02069 67.99217
2Junction Temperature (C) (C) 84.05241 40.58622 9719.54632 77.75637
3Mem Temperature (C) (C) 84.00000 42.00000 8998.64532 71.98916
4VR_GFX (C) (C) 60.00000 33.00000 6346.72336 50.77379
5VR_SOC (C) (C) 58.00000 34.00000 6074.32725 48.59462
6VR_MEM (C) (C) 0.00000 0.00000 0.00000 0.00000
7VR_VDDCI (C) (C) 0.00000 0.00000 0.00000 0.00000
8Liquid0 (C) (C) 0.00000 0.00000 0.00000 0.00000
9Liquid1 (C) (C) 0.00000 0.00000 0.00000 0.00000
10PLX (C) (C) 0.00000 0.00000 0.00000 0.00000
11Min (C) (C) 65.23669 38.50471 7491.50144 59.93201
12GFXCLK Freq () 1553.07312 783.68744 187591.28837 1500.73031
13PWM () 43.73852 0.00000 4269.58901 34.15671
14FAN Speed () 1716.95959 124.55509 167371.82184 1338.97457
15Limit PPT0 (W) (W) 165.00000 165.00000 20625.00000 165.00000
16Value PPT0 (W) (W) 165.00716 9.41742 19857.86155 158.86289
17GFX Activity (%) (%) 97.66265 0.10941 11638.45836 93.10767
18PCIe Link Speed (GT/s) (GT/s)8.00000 8.00000 1000.00000 8.00000
19PCIe Link Width () 16.00000 16.00000 2000.00000 16.00000
20PCIe Correctable Error () 0.00000 0.00000 0.00000 0.00000
21PCIe Uncorrectable Error () 0.00000 0.00000 0.00000 0.00000
22PCIe Residency Gen 1 (%) (%) 60.00000 0.00000 336.84718 2.69478
23PCIe Residency Gen 2 (%) (%) 0.00000 0.00000 0.00000 0.00000
24PCIe Residency Gen 3 (%) (%) 100.00000 40.00000 12163.15287 97.30522
25PCIe Residency Gen 4 (%) (%) 0.00000 0.00000 0.00000 0.00000
26PCIe Residency L0 (%) (%) 100.00000 100.00000 12500.00000 100.00000
27PCIe Residency L0s (%) (%) 0.00000 0.00000 0.00000 0.00000
28PCIe Residency L1 (%) (%) 0.00000 0.00000 0.00000 0.00000
29Fan PWM reading [%] (%) 44.00000 0.00000 4286.00000 34.28800
30mclk (MHz) 876.00000 101.00000 105924.00000 854.22581
31sclk (MHz) 1585.00000 781.00000 186631.00000 1505.08871 本帖最后由 Cool_Breeze 于 2020-3-15 18:42 编辑
#if 0
By Cool_Breeze
Rev 01
#endif
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <float.h>
//返回文件指针
FILE* filepointer(const char *file,const char *mode);
/*
获取文件第一行项目名总数和最大项目名长度存入数组
注意里面有个temp数组大小为1024
*/
void getitemsum(FILE* fp,int *itemsize);
/*
获取文件第一行的项目名放入数组
注意里面有个temp数组大小为1024
*/
void getitemname(FILE* fp,char *name,int num);
/*
读取每一行数据,按格式分割后放入指定数组
注意里面有个temp数组大小为1024
*/
void getdata(FILE* fp,double* max,double* min,double* avg,double* sum,int itemnu);
int main(int argc,char *argv[])
{
//第一个参数是原始文件,第二个参数是结果文件
FILE* sourcep=filepointer(argv,"rb");
FILE* resultp=filepointer(argv,"wb");
//获取文件大小 itemsize储存个数
static int itemsize;
getitemsum(sourcep,itemsize);
//printf("itemnum=%d maxsize=%d\n",itemsize,itemsize);
//开辟项目名空间并赋值
char (*itemname)]=(char(*)])calloc(itemsize*itemsize,sizeof(char));
getitemname(sourcep,itemname,itemsize);
//分配最大值,最小值,求和,平均值空间
double* max=(double*)calloc(itemsize,sizeof(double));
double* min=(double*)calloc(itemsize,sizeof(double));
double* avg=(double*)calloc(itemsize,sizeof(double));
double* sum=(double*)calloc(itemsize,sizeof(double));
//初始化最大值最小值
register int i=0;
for (i=0;i<itemsize;i++) *(min+i)=DBL_MAX, *(max+i)=DBL_MIN;
//获取相应数据
getdata(sourcep,max,min,avg,sum,itemsize);
fprintf(resultp,"%2s%-35s%-16s%-16s%-16s%-16s\n","Nu","Item","Max","Min","Sum","Avg");
for (i=1;i<itemsize;i++) fprintf(resultp,"%2d %-36s%-16.5lf%-16.5lf%-16.5lf%-16.5lf\n",i,*(itemname+i),max,min,sum,avg);
return 0;
}
FILE* filepointer(const char *file,const char *mode)
{
FILE *fp;
if (!(fp = fopen(file,mode)))
{
printf("open fail! %s\n",file);
exit(-2);
}
return fp;
}
void getitemsum(FILE* fp,int *itemsize)
{
char temp={"\0"};//临时储存一行数据
char *tep=temp;
register int i=0;//项目
register int m=0;//最大长度
register int t=0;//临时
fgets(temp,1024,fp);
for (;*tep;tep++)
{
switch (*tep)
{
case ',': i++,t=0;break;//项目加一,长度归零
case '\n': i++;*itemsize=i;*(itemsize+1)=m+1;return;//(m+1)给最后‘\0’留个位置,最后赋值退出
default : t++;
if (t>m) m=t;//始终保持最大长度
}
}
}
void getitemname(FILE *fp,char *name,int num)
{
rewind(fp);
char temp={"\0"};//临时储存一行数据
char *tep=temp;
register int i=0;//第一维
register int j=0;//第二维
fgets(temp,1024,fp);
for (;*tep;tep++)
{
switch (*tep)
{
case ',':i++,j=0;break;
case 10:break;//换行
case 13:break;//回车
default :*(name+i*num+j)=*tep;j++;
}
}
}
void getdata(FILE* fp,double* max,double* min,double* avg,double* sum,int itemnu)
{
rewind(fp);
char temp={"\0"};//临时储存一行数据
char *tep=temp;
register double fdata=0;//储存临时数据
register int i=1;//数组下标(空出第一列数据待后期使用)
register int count=0;//统计行数
fgets(tep,1024,fp);//跳过第一行项目数据
while ( !feof(fp) )
{
count++;
fgets(temp,1024,fp);
i=1;
tep=temp;
while (*tep)
{
if (*(tep++) == ',')
{
fdata=atof(tep);
if (fdata > *(max+i)) *(max+i)=fdata;
if (fdata < *(min+i)) *(min+i)=fdata;
*(sum+i) += fdata;
i++;
}
}
}
//处理最后一行因有可能出现不完整的数据所导致平均数据不准确
register int n;
if (i < itemnu)
{
for (n=1;n<itemnu;n++)
{
if (n >= i) *(avg+n)=*(sum+n)/(count-1);
else *(avg+n)=*(sum+n)/count;
}
}
else for (n=1;n<itemnu;n++) *(avg+n)=*(sum+n)/count;
} 本帖最后由 Cool_Breeze 于 2020-2-20 20:03 编辑
csv文档内容如下:
time stamp, 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)
13:05:30.982, 44.76316, 44.90789, 44.00000, 32.00000, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 41.50000, 796.31171, 0.00000, 170.52246, 165.00000, 32.85556, 0.00000, 8.00000, 16.00000, 0.00000, 0.00000, 0.00000, 0.00000, 100.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 876.0,795.0
13:05:32.007, 45.06062, 45.18257, 44.00000, 32.00000, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 41.59981, 795.48145, 0.00000, 170.52138, 165.00000, 33.75420, 5.10389, 8.00000, 16.00000, 0.00000, 0.00000, 30.00000, 0.00000, 70.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 876.0,796.0
13:05:33.011, 44.37386, 44.41244, 44.00000, 32.00000, 34.20508, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 41.49569, 793.17987, 0.00000, 170.52162, 165.00000, 26.42430, 13.97832, 8.00000, 16.00000, 0.00000, 0.00000, 20.00000, 0.00000, 80.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 501.0,772.0
13:05:34.018, 52.29150, 62.84890, 47.36978, 32.61133, 34.75348, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 45.68688, 1579.41577, 12.67473, 175.16473, 165.00000, 149.90047, 96.07919, 8.00000, 16.00000, 0.00000, 0.00000, 13.33333, 0.00000, 86.66666, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1564.0
13:05:35.022, 55.09911, 66.63048, 52.26693, 33.59263, 35.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 47.97585, 1589.96985, 14.84375, 969.44452, 165.00000, 164.99840, 96.58652, 8.00000, 16.00000, 0.00000, 0.00000, 10.00000, 0.00000, 90.00000, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1612.0
13:05:36.025, 55.81506, 67.23206, 54.00000, 34.48455, 35.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 48.74975, 1599.06433, 14.84375, 980.06689, 165.00000, 165.00172, 95.87738, 8.00000, 16.00000, 0.00000, 0.00000, 8.00000, 0.00000, 92.00000, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1576.0
13:05:37.054, 56.04808, 67.51455, 54.00000, 35.00000, 35.38462, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 49.24310, 1584.18787, 14.84375, 670.29089, 165.00000, 164.99924, 97.36720, 8.00000, 16.00000, 0.00000, 0.00000, 6.66667, 0.00000, 93.33334, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1589.0
13:05:38.071, 56.32507, 68.00413, 55.19534, 35.84062, 36.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 49.70335, 1580.33801, 14.97964, 543.21606, 165.00000, 164.94023, 97.04283, 8.00000, 16.00000, 0.00000, 0.00000, 5.79710, 0.00000, 94.20290, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1580.0
13:05:39.076, 56.74975, 68.43750, 56.00000, 36.00000, 36.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 50.17928, 1591.44324, 15.47805, 630.68823, 165.00000, 165.00043, 95.92555, 8.00000, 16.00000, 0.00000, 0.00000, 5.06329, 0.00000, 94.93671, 0.00000, 100.00000, 0.00000, 0.00000, 16.00000, 876.0,1563.0
13:05:40.082, 57.12053, 68.73981, 56.00000, 36.89761, 36.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 50.59045, 1573.54883, 15.98904, 767.91388, 165.00000, 165.00099, 98.35493, 8.00000, 16.00000, 0.00000, 0.00000, 4.49438, 0.00000, 95.50562, 0.00000, 100.00000, 0.00000, 0.00000, 16.00000, 876.0,1572.0
13:05:41.085, 57.46012, 69.12787, 56.00000, 37.00000, 36.76171, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 50.92173, 1578.60803, 16.50296, 814.84827, 165.00000, 164.99835, 98.22034, 8.00000, 16.00000, 0.00000, 0.00000, 4.04040, 0.00000, 95.95959, 0.00000, 100.00000, 0.00000, 0.00000, 16.00000, 876.0,1574.0
13:05:42.116, 57.74286, 69.49188, 56.00000, 37.62008, 37.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 51.34399, 1576.09644, 17.13105, 778.23401, 165.00000, 165.00224, 97.72848, 8.00000, 16.00000, 0.00000, 0.00000, 3.66972, 0.00000, 96.33028, 0.00000, 100.00000, 0.00000, 0.00000, 17.00000, 876.0,1585.0
13:05:43.149, 58.10330, 69.82226, 57.30941, 38.00000, 37.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 51.73957, 1576.54688, 17.82505, 795.29156, 165.00000, 164.99890, 97.53688, 8.00000, 16.00000, 0.00000, 0.00000, 3.36134, 0.00000, 96.63866, 0.00000, 100.00000, 0.00000, 0.00000, 18.00000, 876.0,1565.0
13:05:44.158, 58.45005, 70.16307, 58.00000, 38.37414, 37.31342, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 52.08913, 1588.35925, 18.52103, 859.73132, 165.00000, 164.99612, 96.32578, 8.00000, 16.00000, 0.00000, 0.00000, 3.12500, 0.00000, 96.87500, 0.00000, 100.00000, 0.00000, 0.00000, 19.00000, 876.0,1608.0
13:05:45.195, 58.72681, 70.47998, 58.00000, 39.00000, 38.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 52.41504, 1577.53979, 19.21621, 889.65686, 165.00000, 165.00304, 97.21816, 8.00000, 16.00000, 0.00000, 0.00000, 2.89855, 0.00000, 97.10145, 0.00000, 100.00000, 0.00000, 0.00000, 20.00000, 876.0,1571.0
13:05:46.229, 59.00315, 70.71370, 58.00000, 39.00000, 38.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 52.76162, 1575.31543, 19.95393, 907.09454, 165.00000, 165.00052, 97.43172, 8.00000, 16.00000, 0.00000, 0.00000, 2.72109, 0.00000, 97.27891, 0.00000, 100.00000, 0.00000, 0.00000, 20.00000, 876.0,1564.0
页:
[1]