calculating average of values from a CSV file in C

﹥>﹥吖頭↗ 提交于 2019-12-12 06:53:26

问题


I am writing a code in which I am reading from a CSV text file which is given as an argument in the command line. I have to calculate the averages of the experiments of the given file:
for example, if the file is

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

I have to print out Bob's experiment (average of numbers) Mary's experiment(average of numbers)

Here is my code:

#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);
}

}

I am getting a weird infinite loop type result. Please tell me what I am doing wrong!

}


回答1:


#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;
}



回答2:


As the above comment suggests, your syntax for checking the value of a character from a FILE* pointer is invalid. You could replace ((char) ptr* != '\n') with (fgetc(ptr) != '\n')


On an additional note, parsing with double nested loops like this is typically bad design and very hard to debug. The infinite loop could be caused by a number of corner cases (e.g. after you've read the last line?). I would suggest having a single while loop with conditions inside for each case e.g.:

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

}

Multiple loops just increase complexity, and are thus better to avoid.


If you absolutely have to use the above algorithm, you could program in safeguards to detect timeouts, e.g.:

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..
}

That way you can easily detect which loop is going into infinite cycles, and determine the state of your variables at that point.



来源:https://stackoverflow.com/questions/26877131/calculating-average-of-values-from-a-csv-file-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!