I have a CSV file formatted as such:
20,Adam,180.00
19,Susan,178.20
18, Jack , 180.00
where each field corresponds to the age, name and height of the person. I'm trying to read that CSV file (called "people.csv") and print out each field (ignoring whitespaces). However I'm having trouble getting that to work properly. At the end of each record, I get "0.00000" printed out, AND, for the last record, Jack's height does not print out at all. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct DATA{
int age;
char name[100];
float height;
};
int main(int argc, char *argv[]){
FILE *fp = fopen("database.csv", "r+");
if(fp == NULL){
printf("Error reading the file.");
return 1;
}
struct DATA records[100];
size_t count = 0;
while(fscanf(fp, "%d,%s,%f", &records[count].age, records[count].name, &records[count].height)==2){
count++;
}
for (int i = 0; i < count; i++) {
printf("%d,%s,%f\n", records[i].age, records[i].name, records[i].height);
}
fclose(fp);
return 0;
}
Also, what should my path be for the filename when testing the code on Eclipse on Windows? Using gcc and bash, it works fine if I keep the CSV file and the C file in the same directory, but I also want to test it out on Eclipse since it's a more powerful IDE than vim or nano. Thanks.