Here is the problem. I am trying to create a doubly linked list using data that is read in from a .csv file(text file with values seperated by commas, i.e.
ADONA,AR,72001,92.90,35.04,494
AGNOS,AR,72513,91.61,36.21,1433)
I don't have any problem finding the file I want and opening it. I think my current problem exists when I am trying to read in data or create the linked list. If someone could either post an algorithm I could follow or find the problems in my code already, I would be much appreciative.
Code:
//Header structure
typedef struct
{
struct Node *psHead;
struct Node *psTail;
int node_count;
}Header;
//Node structure
typedef struct node_d
{
char city[40];
char state[2];
char zipcode[5];
double longitude;//double
double latitude;//double
long population;//long
struct node_d *previous;
struct node_d *next;
}Node;
int choice, file_choice, counter = 2, file_info, readfile, z=-1;
char path_name[100], long_temp[25], lat_temp[25], pop_temp[25], city_temp[40];
char state_temp[2], zipcode_temp[5];
FILE *readfile;
Header *psList;
CSV *file_info;
Node *psnew, *pscurrent;
char *DONE, line[100];
psnew = (Node*) malloc(sizeof(Node));
pscurrent = (Node*) malloc(sizeof(Node));
psList = (Header*) malloc(sizeof(Header));
file_info = (CSV*) malloc(sizeof(CSV));
DONE = (char*)malloc(sizeof(char));
DONE[0] = 'c';
psList->node_count = 0;
psList->psHead = NULL;
psList->psTail = NULL;
readfile = fopen(file_info[file_choice].name,"r");
if(readfile == NULL)
{
printf("\nfile could not be opened");
}
else
{
while ( fgets(line,100,readfile) != NULL )
{
psnew = malloc(sizeof(struct node_d));
z++;
strcpy(psnew->city, strtok(line,","));
strcpy(psnew->state, strtok(NULL,","));
strcpy(psnew->zipcode, strtok(NULL,","));
strcpy(long_temp, strtok(NULL,","));
psnew->longitude = atof(long_temp);
strcpy(lat_temp, strtok(NULL,","));
psnew->latitude = atof(lat_temp);
strcpy(pop_temp, strtok(NULL,","));
psnew->population = atol(pop_temp);
psnew->previous = NULL;
psnew->next = NULL;
psnew->next = pscurrent;
psnew->previous = pscurrent->previous;
psnew->next->previous = psnew;
psnew->previous->next = psnew;
}
}
fclose(readfile);