I am asked to extract rows of a csv file into a linkedlist, every node of the linkedlist will store one row of the data, where the "data" pointer inside the linkedlist points towards a struct, which has every columns of information (like double heading1; char *heading2 etc.) There may be quotes around values that are separated by commas and they should be treated as a whole as the data of that particular column. For example, one field of heading 3 may contain data such as "1, 2, 3" where "1, 2, 3" should be all displayed under heading3, instead of as 3 separate columns.
What I tried is to use two while loops:
//defining data structure here
typedef struct node{
suburb_t *data;
struct node *next;
}Node_t;
//function definition here...
while(fscanf("%d,%d,%d", &x, &y, &z) == 3){
Node_t node = (Node_t *)malloc(sizeof(Node_t));
node->data = (suburb_t *)malloc(sizeof(suburb_t));
node->data->content1 = x
//and so on
while(fscanf(.....) == ...){
//similar to first line
}
//and here I can not figure out how to keep creating new nodes, moving the current
//node to the next node until the outer while loop terminates.
}
The reason why I used double while loop is due to the existence of comma separated values quoted inside quotation marks, which I can then use the second while loop to read the quotation mark, store it inside a variable and read the content as a whole, until the end quotation markk is encountered. We can assume value with quotation marks will not be the first few values...
And my question is how do I create nodes, link it with the previous one iteratively until the first while loop terminates, that is either an EOF is reached or lesser than 3 values are read? My question is mainly towards the end of the code snippet I provided, I think I need to add something within that area but I just don't know what to add....
fscanffunction works. Its first argument is a handler of typeFILE*. And most important, the format%dimplies typeintand neverdoubleorchar.suburb_tstructures.