I have this csv file contain people contact:
NAME, NUMBER, ADDRESS, EMAIL
Kevin Mahendra, +62 812-XXX-XXX, Jln. Anggrek Merah 3 No. 54, kevin@gmail.com
Adwi Lanang, +62 821-XXX-XXX, Jln. Ruhui Rahayu, adwi@gmail.com
and this is the code:
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Number of buckets for TABLE
#define N 26
#define MAX 50
typedef struct Contact
{
char name[MAX];
char number[MAX];
char address[MAX];
char email[MAX];
struct Contact* next;
}
Contact;
// Hash table
Contact *table[N];
int main(void)
{
// OPEN CSV FILE
FILE *file = fopen("contacts.csv", "r");
if (file == NULL)
{
printf("Error open file!\n");
return 1;
}
while(!feof(file))
{
Contact *new = malloc(sizeof(Contact));
fscanf(file, "%[^,],%[^,],%[^,],%[^,]", new->name, new->number, new->address, new->email);
printf("%s,%s,%s,%s\n", new->name, new->number, new->address, new->email);
int index = hash(new->name);
new->next = table[index];
table[index] = new;
}
}
As you can see, i try to put each person contact into hash table. But it fails to read csv file properly. Because when i try to printf it, the result is become infinite loop.
BUT it's working when my csv file only contain one line/row. It will printf the contact inside csv properly. But when i add a new contact/row into csv file, it fails to read.
How can i read csv file one line/row at a time? and maybe also skip the header NAME, NUMBER, ADDRESS, EMAIL
? Thank you!