简体   繁体   English

无法将文件中的数据读入链表

[英]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结果在这段代码中我将 memory 分配在错误的位置,我应该在“for”循环内分配,这样数据就不会被覆盖

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.首先我们不知道user_t长什么样。 If it is like如果是这样

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

Then temp = (user_t*)malloc(sizeof(user_t));然后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:实际上并没有为您提供任何名称空间 - 您需要另一个 malloc (或使用strdup而不是strcpy )或将空间直接放入结构中:

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

Next: lines like this shouldn't even compile: char name = "";下一个:这样的行甚至不应该编译: char name = ""; as the types are wrong.因为类型是错误的。 The variable is of type char but you are assigning a string to it.该变量是 char 类型,但您正在为其分配一个字符串。 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.下一步:您永远不会检查mallocfopen是否正常工作 - 两者都可能失败。 Sure the malloc isn't likely to fail, but the file open might - continuing on when either one has failed is undefined behavior.当然 malloc 不太可能失败,但文件打开可能 - 在任何一个失败时继续是未定义的行为。

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);下一步: scanf需要读取的地址,所以应该是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);您可以通过直接读入“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. (请注意,这假设您已经解决了关于 struct 的第一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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