用C语言计算CSV文件中值的平均值
我正在编写一个代码,其中我正在从命令行中作为参数提供的CSV文本文件中读取数据。我必须计算给定文件的实验平均值:用C语言计算CSV文件中值的平均值,c,file,csv,average,fileparsing,C,File,Csv,Average,Fileparsing,我正在编写一个代码,其中我正在从命令行中作为参数提供的CSV文本文件中读取数据。我必须计算给定文件的实验平均值: 例如,如果文件是 Bob's experiment,12,33,55,8 Mary's experiment,99,21,12,0 我得打印出来 鲍勃的实验(数的平均值) 玛丽的实验(平均数) 这是我的密码: #include<stdio.h> #include<stdlib.h> #include<stdlib.h> #include<s
例如,如果文件是
Bob's experiment,12,33,55,8
Mary's experiment,99,21,12,0
我得打印出来
鲍勃的实验(数的平均值)
玛丽的实验(平均数)
这是我的密码:
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
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);
}
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
文件*ptr=fopen(argv[1],“rt”);
int i=0;
双和=0;
重复计数=0;
双平均值=0;
如果(ptr==NULL){
perror(“打开文件时出错”);
退出(退出失败);
}
而(!feof(ptr)){
chars='a';
而{
s=fgetc(ptr);
printf(“%c”,s);
}
而((字符)*ptr)!='\n'){
fscanf(ptr、%d、&i);
总和+=i;
计数++;
}
ave=总和/计数;
printf(“%.2f”,ave);
}
fclose(ptr);
}
}
我得到了一个奇怪的无限循环类型的结果。
请告诉我我做错了什么
}如上所述,用于检查文件*指针中字符值的语法无效。您可以将((char)ptr*!='\n')替换为(fgetc(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..
}
int timeout=0;
而(s!=','&超时<500){
...
超时++;
}
如果(超时>=500){
printf(“第一个循环超时,s:%c\n”,s);
…如果您愿意,还可以进行一些其他有用的状态检查。。
}
这样,您就可以轻松地检测哪个循环进入无限循环,并确定此时变量的状态。\include
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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;
}
#包括
#包括
int main(int argc,char*argv[]){
文件*ptr=fopen(argv[1],“rt”);
双和,计数,平均值;
int i=0;
如果(ptr==NULL){
perror(“打开文件时出错”);
退出(退出失败);
}
而(1){
int-s;
而((s=fgetc(ptr))!='、'&&s!=EOF){
printf(“%c”,s);
}
如果(s==EOF)
打破
printf(“\t”);
计数=总和=0;
而(1==fscanf(ptr,“%d%*c”,&i)){//%*c skip',和'\n'
总和+=i;
计数++;
}
ave=总和/计数;
printf(“%.2f\n”,ave);
}
fclose(ptr);
返回0;
}
这看起来可疑:而(((char)*ptr)!='\n')
。取消引用指向文件的指针,然后尝试将其与字符进行比较似乎是不正确的。前面的while
——如果文件中没有逗号或换行符,会发生什么情况?您知道如何修复它吗。在最后一个while循环中,我尝试使用while循环来说明指针没有到达行的末尾文件*
不是指向文件内容的指针-它只是文件的句柄。您必须阅读内容(例如使用fgetc
或fscanf
),然后才能根据内容做出任何决定。