Cannot find error in string manipulations / pointers

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(). .

+3
4

, node, , line, , fgets(), . , node ( ).

( ) strdup() , node. - :

....
} else {
    addnode = (struct customer *) malloc(sizeof(struct customer));
    addnode->name = strdup(name);
    addnode->streetaddress = strdup(streetaddress);
    addnode->city = strdup(city);
    addnode->state = strdup(state);
    addnode->zip = strdup(zip);
    addnode->balance = strdup(balance);
    ...
}

node , :

   addnode = (struct customer *) malloc(sizeof(struct customer));
   memset(addnode, '\0', sizeof(struct customer));
+1
  • "string" . , .
  • strtok , . , .
+2

, , struct customer* addnode = NULL; add_node(), (.. , , ). char, , gotcha,

, , :

while(!feof(file)) {

   name = strtoke(string, ",");
   // etc

   add_node(name, ... //etc);
}

, .. addnode->name = name, , , , .

+1
source

As Dark Falcon points out, the * add_node * code is incorrect. You need to do something like this:

  void add_node(char* name, char* streetaddress, char* city, char* state, int zip, float balance){
    struct customer* addnode = NULL;

    strip_spaces(name);
    strip_spaces(streetaddress);
    strip_spaces(city);
    strip_spaces(state);

    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;
  }

and then call it like this:

    add_node(strdup(name), strdup(streetaddress), strdup(city), strdup(state), zip, balance);
0
source

Source: https://habr.com/ru/post/1778225/


All Articles