I have a csv file that looks like this:
Jake, 25, Montreal
Maria, 32, London
Alex, 19, New York
Jake, 22, Dubai
The function I'm trying to implement is find_name that should iterate through the first field of each record and compare it to the name that is being searched for.
I've tried fgets, fscanf, but either the code doesn't work or I get a segmentation fault.
This is what I have so far:
void find_name(const char *csv_filename, const char *name){
FILE *csvFile = fopen(csv_filename, "r");
char word[1000];
if (csvFile == NULL)
exit(EXIT_FAILURE);
while ( !feof(csvFile) ) {
fscanf(csvFile, "%s%*[^,]", word);
if ( strcmp(word, name) == 0 )
printf("name found");
}
fclose(csvFile);
}
Any help is appreciated.
EDIT: I would not like to use any tokenizer function, I'd rather understand how to use fscanf.