Tag: fileparsing

计算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; […]