[英]Linked list issue from reading a file c
我尝试从文件设计链表,但是设置链表的“第一个节点”时存在一些问题。 链表的数据结构如下图所示:
在我的代码中,我放置了两种类型的结构,一种用于名称,另一种用于得分,如下所示:
typedef struct nodeScore{
int score;
struct nodeScore* nextScore;
}nodeScore;
typedef struct nodeName{
char* firstName;
char* lastName;
nodeScore* nextScore;
struct nodeName* nextName;
}nodeName;
还有一个功能,可以逐行从文件中读取分数(每行包含“名字”,“姓氏”和最多4个“分数”),并返回链接列表的开头:
nodeName* storeFile(const char* fileName, nodeName* nodeN){
FILE *pFile = fopen(fileName, "r");
char input[512];
char* firstName;
char* lastName;
int score;
int line=0;
nodeName* prevName;
nodeScore* prevScore;
while(fgets(input, 512, pFile)){
printf("Line %d now.\n", ++line);
nodeName* ptrN = (nodeName*)malloc(sizeof(nodeName));//allocate for new node name.
firstName = strtok(input, " ");//first space for first name.
ptrN->firstName = firstName;
lastName = strtok(NULL, " ");//second space for last name.
ptrN->lastName = lastName;
if(line == 1){
prevName = ptrN;
nodeN = ptrN;//allocate nodeN the return value to the first line, first node of the linked list.
}else{
prevName->nextName = ptrN;
prevName = ptrN;
}
ptrN->nextName = NULL;
ptrN->nextScore = NULL;
while(score = atoi(strtok(NULL, " \n"))){//store multiple scores until next char is space or next new line.
if(ptrN->nextScore == NULL){//if no link to another score.
nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore));//allocate for new score node.
ptrN->nextScore = ptrS;
ptrS->score = score;
ptrS->nextScore = NULL;
prevScore = ptrS;//record latest 'tail' of linked list.
}else{
nodeScore* ptrS = (nodeScore*)malloc(sizeof(nodeScore));
prevScore->nextScore = ptrS;
ptrS->score = score;
ptrS->nextScore = NULL;
prevScore = ptrS;//record latest 'tail' or linked list.
}
}//done the loop for storing multi-scores.
}//done the loop for reading lines from file.
return nodeN;
}
最后是主要功能:
int main(){
char file1[]={"HW5_26.txt"};
nodeName* n1;
printf("\nStart reading file '%s' and store it into linked-list.\n\n", file1);
n1 = storeFile(file1, n1);
printf("%s", n1->firstName);//try to see who's the first person(1st node, should be Albert Einstein), here is when I'm confused.
return 0;
}
在返回值的最后,我总是得到最后一个节点“ Sarah”的结果,但是,我已经设置了“ if过滤器”以仅从第一行数据获取返回值,我不知道哪个部分出错了,如果有人可以给我一些建议或想法,我将非常感谢,谢谢。
txt文件是这样的:
Albert Einstein 52 67 63
Steve Abrew 90 86 90 93
David Nagasake 100 85 93 89
Mike Black 81 87 81 85
Andrew Dijkstra 90 82 95 87
Joanne Nguyen 84 80 95 91
Chris Walljasper 86 100 96 89
Fred Albert 70 68
Dennis Dudley 74 79 77 81
Leo Rice 95
Fred Flinstone 73 81 78 74
Frances Dupre 82 76 79
Dave Light 89 76 91 83
Hua Tran 91 81 87 94
Sarah Trapp 83 98 94 93
和我的标题:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
问题是如何使用strtok
和返回的指针:
firstName = strtok(input, " ");//first space for first name.
ptrN->firstName = firstName;
指针firstName
将是指向数组input
的指针。 整个循环的数组input
是相同的,这意味着所有名称的所有指针都将指向同一数组。 这也意味着,一旦数组不再存在,则storeFile
函数返回后,这些指针将变为无效,并且使用这些指针将导致未定义的行为 。
有两种可能的解决方案:一种是使用例如strdup
复制字符串。 另一种是在名称的结构中包含数组,然后复制字符串。
请注意, strdup
不是标准的C函数,但是几乎所有系统都具有它。 另请注意,它将使用malloc
动态分配内存,因此您需要在free
节点之前free
字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.