简体   繁体   中英

Can't read data from file into a linked list

Ok I have already managed to solve my problem, thanks to all who replied. Turns out in this code I'm allocating memory in the wrong place, I should be allocating inside the 'for' loop so that the data is not overwritten

void getdata (int counter){
    int i = 0;
    user_t* temp = NULL, * ptr = NULL;
    //temp = (user_t*)malloc(sizeof(user_t)); this is what I was doing
    FILE *varfile;  
    varfile = fopen ("data.txt", "r");
    if (varfile==NULL) {
        printf("Error");
        return;
    }
    else { 
        for (i = 0; i < counter; i++){
            temp = (user_t*)malloc(sizeof(user_t)); //and this is where I should be allocating
            fscanf (varfile, "%d %s %s %s %d %d %d %f", &temp->id, temp->name, temp->birth_place, temp->work_place, &temp->prof_obj, &temp->academics, &temp->hobby, &temp->salary);
            temp->prox = NULL; 
            if (start == NULL) {
                start = temp;
            }
            else {
                ptr = start;
                while (ptr->prox != NULL) {
                    ptr = ptr->prox;
                }
            ptr->prox = temp;
            }
        }
    }
    fclose (varfile);
    return;
}

There are some obvious problems with your code.

First off we don't know what user_t looks like. If it is like

typedef struct user {
    ...
    char* name;
    ...
} user_t;

Then temp = (user_t*)malloc(sizeof(user_t)); doesn't actually give you any space for the name - you would need another malloc (or use strdup instead of strcpy ) or put the space straight into the struct:

typedef struct user {
    ...
    char name[64];
    ...
} user_t;

Next: lines like this shouldn't even compile: char name = ""; as the types are wrong. The variable is of type char but you are assigning a string to it. char* name = ""; would compile but is still wrong. You are using these variables as buffers to read a string into. You need space to store the string. char name[64]; will do - but obviously you need the size to be bigger than your biggest expected name.

Next: You never check the malloc or fopen worked - both could potentially fail. Sure the malloc isn't likely to fail, but the file open might - continuing on when either one has failed is undefined behavior.

Next: scanf needs the address of where to read to, so should be like fscanf(fp, "%d %s %s %s %d %d %d %f", &id, name, birth_place, work_place, &prof_obj, &academics, &hobby, &salary); Note the string buffers are already addresses so you don't need the ampersand on them.

You can avoid the use of temporary variables and string copies by reading straight into "temp": fscanf(fp, "%d %s %s %s %d %d %d %f", &temp->id, temp->name, temp->birth_place, temp->work_place, &temp->prof_obj, &temp->academics, &temp->hobby, &temp->salary); (note this assumes you've addressed the first issue about the struct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM