I have a programming assignment that gives a data file “candidatesdata.txt” You are supposed to read information in and decipher what is the name, gender, height, and weight. The only problem is that there are quotations around the name and commas in between each data record. The file goes as follows:
Name,Gender,Height,Weight
"Tanner, Mark C.”,M,71.8,180.25
"Jinglehiemmerschmitt, John J.”,M,70.75,185.3
"Parker, Sarah J.",F,65.25,120.3
..cont.
How do I read the data records and ignore the quotations and the commas? This is what I have so far, it deletes some commas and quotations but also in the process it deletes the names.
#include <stdio.h>
struct candidateinfo
{
char name[50];
char gender;
double height;
double weight;
}candidate;
int main()
{
int count =0;
FILE *candidate_data;
// Open file
candidate_data = fopen("/Users/moisestrevino/Documents/CS 1324/Assignment #5/Assignment #5/candidatedata.txt","r");
fscanf(candidate_data, "%*[^\n]");
fgetc(candidate_data);
// Get rid of unecessary letters
while (count<40)
{
count++;
fscanf(candidate_data,"\"%[^\"],%c,%lf,%lf\n", candidate.name, &candidate.gender, &candidate.height, &candidate.weight);
printf("%s %c %lf %lf\n",candidate.name, candidate.gender, candidate.height, candidate.weight);
}
fclose(candidate_data);
return 0;
}
Output:
Tanner, Mark C. 0.000000 0.000000
,M,71.8,180.25
0.000000 0.000000
Jinglehiemmerschmitt, John J. 0.000000 0.000000
,M,70.75,185.3
0.000000 0.000000
Parker, Sarah J. 0.000000 0.000000
,F,65.25,120.3
0.000000 0.000000
Meeks, Kalvin R. 0.000000 0.000000
,M,57.25,210.2
cont...