So, firstly, this is homework, so please do not write any code for me, just indicate where my code is incorrect.
The basics of code are the address / balance book. I have a structure with my variables, but for some reason my doubly linked list is all messed up and I can’t figure out how to do this. With a bit of fantasy (not really) debugging, I realized that I think fgets (line, 200, file); the line rewrites my head-> name pointer somehow, which seems to discard the rest of the code. Relevant code snippets here:
List Filling:
void populate_list(char* filename){
FILE *file = NULL;
char* name = NULL;
char* streetaddress = NULL;
char* city = NULL;
char* state = NULL;
char line[200];
int zip;
float balance;
file = fopen(filename,"r");
if(file==NULL){
printf("Invalid input file\n");
exit(1);
}
if(file == 0){
printf("Invalid file.\n");
exit(1);
}
while(!feof(file)){
fgets(line, 200, file);
name = strtok(line, ",");
streetaddress = strtok(NULL, ",");
city = strtok(NULL,",");
state = strtok(NULL,",");
zip = atoi(strtok(NULL,","));
balance = atof(strtok(NULL,","));
strip_spaces(name);
strip_spaces(streetaddress);
strip_spaces(city);
strip_spaces(state);
add_node(name, streetaddress, city, state, zip, balance);
}
fclose(file);
return;
}
Then add_node code:
void add_node(char* name, char* streetaddress, char* city, char* state, int zip, float, balance){
struct customer* addnode = NULL;
if(find_duplicate(name)){
print_filler(1);
printf("DUPLICATE RECORD: %s\n", name);
return;
} else {
addnode = (struct customer *) malloc(sizeof(struct customer));
addnode->name = name;
addnode->streetaddress = streetaddress;
addnode->city = city;
addnode->state = state;
addnode->zip = zip;
addnode->balance = balance;
if(head == NULL) {
head = addnode;
addnode->prev = NULL;
} else {
tail->next = addnode;
addnode->prev = tail;
}
tail = addnode;
addnode->next = NULL;
}
print_list();
return;
}
, add_node , fgets() . - head- > fgets().
, , , , .
, : http://pastebin.com/k0pqyvT0
, , head- > , fgets(), , , , / .
: strdup() , add_node(). .