I need to read in a huge CSV file and store it in a struct, to be inserted into a binary search tree (but that's another story) however I can only get the first four columns to be read in (one line has four columns)- any ideas?
Intended output:
Michael,21,M,12345
Susan,34,F,13342
Robin,12,F,45423
Teddy,43,M,56231
Ed,21,M,34354
Ramirez,21,F,79563
Toby,32,M,34277
Guy,23,M,90765
Desiree,21,F,67453
Molly,24,F,45434
Alison,23,F,62356
Tara,26,F,45223
Sally,21,F,34545
Mike,20,M,67676
Roger,19,M,83737
David,21,M,78554
Davis,23,M,66554
Sheng,22,M,31211
Wesley,31,M,21436
etc. but I am getting
Michael,21,M,12345
Susan
Code:
#include <stdlib.h>
#include <stdio.h>
#define MAXLEN 128
typedef struct{
char name[MAXLEN];
char age[MAXLEN];
char sex[MAXLEN];
char courseID[MAXLEN];
}input_t;
typedef struct{
input_t input;
struct bst *left;
struct bst *right;
} bst;
void parse_csv(bst *node);
void print_csv(bst node);
void parse_csv(bst *node){
while (scanf(" %[^,],%[^,],%[^,],%[^,]\n", node->input.name, node->input.age, node->input.sex, node->input.courseID) == 4) {
break;
}
}
void print_csv(bst node){
printf("%s,%s,%s,%s\n",node.input.name, node.input.age, node.input.sex, node.input.courseID);
}
int main(int argc, char *argv[]) {
bst node;
parse_csv(&node);
print_csv(node);
return 0;
}
how the csv looks like, we will be given bigger test files to test on, though