从文本文件创建链接列表

时间:2017-03-15 22:17:14

标签: c malloc singly-linked-list

无法读取文本文件并将其存储在链接列表中。我正在读一个名字,名字,优先级和阅读水平的文件。我认为问题可能是我没有正确地对字符串/ tempPtr进行malloc,或者我的循环不断运行。运行程序时,它会持续运行,没有seg故障。

Greeting = string.Format("My Name is: {0}", myName);

1 个答案:

答案 0 :(得分:0)

我建议您更改student结构,以便firstNamelastName是数组而不是指针。

,当它们的大小始终相同时,不需要使用动态分配。

typedef struct Student{
    char firstName[NAME];
    char lastName[NAME];
    int priority;
    int readingLevel;
    bookIds* wishlist;
    struct Student* next;
}student;

接下来,每当您分配新的student时,都需要初始化wishListnext指针{。}}。

当您致电NULL时,您需要将指针传递给您正在填写的fscanf()成员。

您可以在一个地方而不是在循环之前执行int来简化代码,然后在malloc()的每个分支中重复它。读取局部变量的输入,然后在成功后分配if

student
相关问题