
C语言读CSV文件问题
现在有一个CSV文件如下:1,20080101,陈,男,30.3,16.7,42.8,81.1,90.21,20080102,李,男,51.6,92.8,23.1,40....
现在有一个CSV文件如下:
1,20080101,陈,男,30.3,16.7,42.8,81.1,90.2
1,20080102,李,男,51.6,92.8,23.1,40.6,21.6
1,20080103,张,男,94,16.5,56.1,63.6,69
1,20080104,王,男,73,63.9,65.1,44.9,41.7
1,20080105,赵,女,54.4,28.2,67.2,42.4,18.9
1,20080106,钱,女,23,52.4,70.5,73,40
1,20080107,孙,女,12.6,87.8,77,64.4,49.8
1,20080108,吴,女,59,14.5,47.8,37.2,38.2
2,20080201,陈,女,83,69.3,14.2,28.3,65.3
2,20080202,李,女,21.6,51.7,64.6,71.2,58.6
2,20080203,张,女,20.3,7.4,15.6,99.1,48.9
2,20080204,王,女,7.4,20.9,42,66.6,62.5
2,20080205,赵,男,97.6,78.7,54.4,6.7,84.4
2,20080206,钱,男,70.8,40.7,57,58.5,98.1
2,20080207,孙,男,86.4,53.2,21.4,22.6,85.8
2,20080208,吴,男,36.4,66.1,59.6,34.7,54.7
我编写一段程序读文件,然后统计分数:
FILE *fp; /*定义一个文件指针*/
FILE *cp; /*定义一个文件指针*/
struct sum_type qq;
double sx=0,yw=0,wl=0,hx=0,yy=0;
int i;
char str[20];
char *str1,*stt[8];
fp=fopen("分数表.csv", "rb"); /*打开文件只读*/
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s/n",str);
str1=strtok(str,",");
for(int j=0;j<8;j++)
{
stt[j]=strtok(NULL,",");
}
printf("%s ",stt[6]);
printf("%s\n",stt[7]);
sx+=atof(stt[3]);
yw+=atof(stt[4]);
wl+=atof(stt[5]);
hx+=wl+atof(stt[6]);
yy+=wl+atof(stt[7]);
}
printf("%f,%f,%f,%f,%f\n",sx,yw,wl,hx,yy);
结果打印出的结果:
81.1 90.2
40.6 21.6
63.6 69
44.9 41.7
42.4 18.9
73 40
64.4 49.8
37.2 38.2
28.3 65.3
71.2 58.6
99.1 48.9
66.6 62.5
6.7 84.4
58.5 98.1
22.6 85.8
34.7 54.7
821.400000,760.800000,778.400000,34.700000,0.000000
其中stt[6]可以读出来,但加算的结果为什么不正确呢?请高手帮忙看看.
上边的错了,代码中的是:
hx+=atof(stt[6]);
yy+=atof(stt[7]);
最终着两个加算的结果出来是问题的,而前3个数出力是没问题的 展开
1,20080101,陈,男,30.3,16.7,42.8,81.1,90.2
1,20080102,李,男,51.6,92.8,23.1,40.6,21.6
1,20080103,张,男,94,16.5,56.1,63.6,69
1,20080104,王,男,73,63.9,65.1,44.9,41.7
1,20080105,赵,女,54.4,28.2,67.2,42.4,18.9
1,20080106,钱,女,23,52.4,70.5,73,40
1,20080107,孙,女,12.6,87.8,77,64.4,49.8
1,20080108,吴,女,59,14.5,47.8,37.2,38.2
2,20080201,陈,女,83,69.3,14.2,28.3,65.3
2,20080202,李,女,21.6,51.7,64.6,71.2,58.6
2,20080203,张,女,20.3,7.4,15.6,99.1,48.9
2,20080204,王,女,7.4,20.9,42,66.6,62.5
2,20080205,赵,男,97.6,78.7,54.4,6.7,84.4
2,20080206,钱,男,70.8,40.7,57,58.5,98.1
2,20080207,孙,男,86.4,53.2,21.4,22.6,85.8
2,20080208,吴,男,36.4,66.1,59.6,34.7,54.7
我编写一段程序读文件,然后统计分数:
FILE *fp; /*定义一个文件指针*/
FILE *cp; /*定义一个文件指针*/
struct sum_type qq;
double sx=0,yw=0,wl=0,hx=0,yy=0;
int i;
char str[20];
char *str1,*stt[8];
fp=fopen("分数表.csv", "rb"); /*打开文件只读*/
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s/n",str);
str1=strtok(str,",");
for(int j=0;j<8;j++)
{
stt[j]=strtok(NULL,",");
}
printf("%s ",stt[6]);
printf("%s\n",stt[7]);
sx+=atof(stt[3]);
yw+=atof(stt[4]);
wl+=atof(stt[5]);
hx+=wl+atof(stt[6]);
yy+=wl+atof(stt[7]);
}
printf("%f,%f,%f,%f,%f\n",sx,yw,wl,hx,yy);
结果打印出的结果:
81.1 90.2
40.6 21.6
63.6 69
44.9 41.7
42.4 18.9
73 40
64.4 49.8
37.2 38.2
28.3 65.3
71.2 58.6
99.1 48.9
66.6 62.5
6.7 84.4
58.5 98.1
22.6 85.8
34.7 54.7
821.400000,760.800000,778.400000,34.700000,0.000000
其中stt[6]可以读出来,但加算的结果为什么不正确呢?请高手帮忙看看.
上边的错了,代码中的是:
hx+=atof(stt[6]);
yy+=atof(stt[7]);
最终着两个加算的结果出来是问题的,而前3个数出力是没问题的 展开
展开全部
DEBUG的时候可以发现,第一次循环时,执行到yy+=atof(stt[7]); 时,stt[7]的值突然被改变了,第二次循环时,甚至只执行到hx+=atof(stt[6]); stt[6]也被改变了.
我认为问题是由于strtok是不安全所导致的,它生成的字符串数组可能被其他线程修改.
我建议你用sscanf来读取字符串中的数字,比如:
#include "string.h"
#include "math.h"
void main()
{
FILE *fp; /*定义一个文件指针*/
double sx,yw,wl,hx,yy;
int i;
char str[81];
double score[5];
sx=yw=wl=hx=yy=0;
fp=fopen("分数表.csv", "rb"); /*打开文件只读*/
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s\n",str);
sscanf(str+17,"%lf,%lf,%lf,%lf,%lf",score,score+1,score+2,score+3,score+4);
sx+=score[0];
yw+=score[1];
wl+=score[2];
hx+=score[3];
yy+=score[4];
}
printf("%f,%f,%f,%f,%f\n",sx,yw,wl,hx,yy);
fclose(fp);
}
我认为问题是由于strtok是不安全所导致的,它生成的字符串数组可能被其他线程修改.
我建议你用sscanf来读取字符串中的数字,比如:
#include "string.h"
#include "math.h"
void main()
{
FILE *fp; /*定义一个文件指针*/
double sx,yw,wl,hx,yy;
int i;
char str[81];
double score[5];
sx=yw=wl=hx=yy=0;
fp=fopen("分数表.csv", "rb"); /*打开文件只读*/
for(i=0;!feof(fp);i++)
{
fscanf(fp,"%s\n",str);
sscanf(str+17,"%lf,%lf,%lf,%lf,%lf",score,score+1,score+2,score+3,score+4);
sx+=score[0];
yw+=score[1];
wl+=score[2];
hx+=score[3];
yy+=score[4];
}
printf("%f,%f,%f,%f,%f\n",sx,yw,wl,hx,yy);
fclose(fp);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询