计算C中CSV文件的平均值

我正在编写一个代码,我在其中读取CSV文本文件,该文件在命令行中作为参数提供。 我必须计算给定文件的实验平均值:
例如,如果文件是

Bob's experiment,12,33,55,8 Mary's experiment,99,21,12,0 

我必须打印鲍勃的实验(数字的平均值)玛丽的实验(数字的平均值)

这是我的代码:

 #include #include #include #include int main (int argc, char *argv[]){ FILE* ptr=fopen(argv[1], "rt"); int i=0; double sum=0; double count=0; double ave=0; if (ptr==NULL){ perror("Error while opening file"); exit(EXIT_FAILURE); } while(!feof(ptr)){ char s='a'; while(s!=','){ s=fgetc(ptr); printf("%c", s); } while((char) *ptr)!='\n'){ fscanf(ptr, "%d", &i); sum+=i; count++; } ave=sum/count; printf("%.2f", ave); } fclose(ptr); } 

}

我得到一个奇怪的无限循环类型结果。 请告诉我我做错了什么!

}

 #include #include #include int main (int argc, char *argv[]){ FILE* ptr=fopen(argv[1], "rt"); double sum, count, ave; int i=0; if (ptr==NULL){ perror("Error while opening file"); exit(EXIT_FAILURE); } while(1){ int s; while((s=fgetc(ptr)) != ',' && s != EOF){ printf("%c", s); } if(s == EOF) break; printf("\t"); count = sum = 0; while(1==fscanf(ptr, "%d%*c", &i)){//%*c skip ',' and '\n' sum += i; count++; } ave = sum / count; printf("%.2f\n", ave); } fclose(ptr); return 0; } 

如上面的注释所示,从FILE *指针检查字符值的语法无效。 你可以用(fgetc(ptr)!=’\ n’)替换((char)ptr *!=’\ n’)


另外需要注意的是,使用这样的双嵌套循环进行解析通常是糟糕的设计并且很难调试。 无限循环可能是由多个极端情况引起的(例如,在您读完最后一行之后?)。 我建议在每种情况下都有一个带有条件的while循环,例如:

 while(!feof(ptr)) { char s = fgetc(ptr); if(s == '\n') { ... } else if(s == ',') { ... } else { ... } } 

多个循环只会增加复杂性,因此更好地避免。


如果您必须使用上述算法,则可以编写安全措施来检测超时,例如:

 int timeout = 0; while(s!=',' && timeout < 500) { ... timeout++; } if(timeout >= 500) { printf("First loop timeout, s:%c\n", s); ... some other useful state checking if you wish.. } 

这样,您可以轻松地检测哪个循环进入无限循环,并确定该点的变量状态。