重新分配了第一个节点的新数据

时间:2019-04-03 10:35:15

标签: c linked-list

我已经创建了linked list,现在我正尝试从文件中读取数据,但是每次读取新条目并将其分配给该结构时,都会重新分配第一个节点并从先前条目中删除数据。我想找出为什么我的代码这样做了。

/* read file containing database of employees */
static void read_employee_database(char *file_name)
{
    char Name[MAX_NAME_LENGTH] = "\0";
    int Age;
    char Sex;
    char Job[MAX_JOB_LENGTH] = "\0";
    char ptr[10];
    char temp[100];
    int test = 0, count = 0;
    /*assigns enough memory*/
    Employee *pNewStruct = (Employee *)malloc(sizeof(Employee));



    FILE *pFile;    

    pFile = fopen(file_name, "r");
    while (feof(pFile) == 0) {
        fprintf(stderr, "\nFirst node name %s\n\n", pFirstNode->name);


        read_string(pFile, "Name: ", Name, MAX_NAME_LENGTH);
        strcpy(pNewStruct->name, Name);
        read_string(pFile, "Sex: ", temp, 4);
        Sex = temp[0];
        pNewStruct->sex = Sex;
        read_string(pFile, "Age: ", temp, 100);
        Age = atoi(temp);
        pNewStruct->age = Age;
        read_string(pFile, "Job: ", Job, MAX_JOB_LENGTH);
        strcpy(pNewStruct->job, Job);

        fprintf(stderr, "The name is: %s \n", Name);
        fprintf(stderr, "The Age is: %i \n", Age);
        fprintf(stderr, "The Sex is: %c \n", Sex);
        fprintf(stderr, "The Job is: %s \n", Job);
        fprintf(stderr, "\n");

        fgetc(pFile);
        /*Test all data for given entry*/
        test = checkName(&pNewStruct->name);
        if (test == 0) {
            fprintf(stderr, "Name is issue");
            exit(-1);
        }
        else if (pNewStruct->sex != 'M' && pNewStruct->sex != 'F') {
            fprintf(stderr, "Sex is issue");
            exit(-1);
        }
        else if (pNewStruct->age <= 0) {
            fprintf(stderr, "Age is issue");
            exit(-1);
        }
        else if (strlen(pNewStruct->job) == 0) {
            fprintf(stderr, "Job is issue");
            exit(-1);
        }
        else {

            if (pFirstNode->name == NULL) {
                fprintf(stderr, "a new node is created\n\n");
                pNewStruct->next = NULL;
                pNewStruct->prev = NULL;
                pFirstNode = pNewStruct;
            }
            else {
                fprintf(stderr, "Else statement run\n\n");
            }
        }
    }

    fclose(pFile);
}

在代码的这一点上,我只希望给定第一个节点,因为“ pFirstNode”是public variable的{​​{1}}。但是,即使不满足NULL语句条件,也会自动将最新读取的条目设置为pFirstNode

实际输出:

if

如果重要的话,我正在使用Ubuntu。

1 个答案:

答案 0 :(得分:1)

  

读取第一个已重新分配的节点的新数据

您只分配了一个 Employee ,所以您一直在重写它,您需要为每个单元格分配一个

Employee *pNewStruct = (Employee *)malloc(sizeof(Employee));

必须在 while 内移动,例如紧接在

之后
read_string(pFile, "Name: ", Name, MAX_NAME_LENGTH);

您将在列表的末尾添加单元格以遵守顺序,为此添加一个指向最后一个单元格的变量(例如 pLastCell

       if (pFirstNode->name == NULL) {
           fprintf(stderr, "a new node is created\n\n");
           pNewStruct->next = NULL;
           pNewStruct->prev = NULL;
           pFirstNode = pNewStruct;
       }

添加pLastCell = pNewStruct;

        else {
           fprintf(stderr, "Else statement run\n\n");
       }

您需要链接其他单元格:

  pLastCell->next = pNewStruct;
  pNewStruct->prev = pLastCell;
  pLastCell = pNewStruct;
  pNewStruct->next = NULL;

请勿使用 feof ,在read_string中检测文件结尾,还应处理文件无效且缺少某些字段的情况

例如:

while (read_string(pFile, "Name: ", Name, MAX_NAME_LENGTH))

read_string在EOF /错误时返回0

删除fprintf(stderr, "\nFirst node name %s\n\n", pFirstNode->name);毫无意义