我尝试从文件设计链接列表,但在设置'第一个节点时遇到了一些问题。链表。链表的数据结构如下图所示:
在我的代码中,我放了两种类型的结构,一种用于名称,另一种用于得分,如下所示:
typedef struct nodeScore{
int score;
struct nodeScore* nextScore;
}nodeScore;
typedef struct nodeName{
char* firstName;
char* lastName;
nodeScore* nextScore;
struct nodeName* nextName;
}nodeName;
还有一个功能是逐行读取文件中的分数(每行包含'名字','姓氏'以及最多4&#39 ;得分'),并返回链表头部:
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'的结果。最后一个节点,但是,我已经设置了一个过滤器'为了只从第一行数据中获取返回值,我不知道哪个部分出错了,如果有人能给我一些建议或想法,我会非常感激,谢谢。
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>
答案 0 :(得分:3)
问题是如何使用strtok
和返回的指针:
firstName = strtok(input, " ");//first space for first name.
ptrN->firstName = firstName;
指针firstName
将是指向数组input
的指针。数组input
对于整个循环是相同的,这意味着所有名称的所有指针都将指向同一个数组。 也的含义是,一旦storeFile
函数返回,这些指针将变为无效,因为数组将不再存在,并且使用这些指针将导致未定义的行为
有两种可能的解决方案:一种是使用例如strdup
复制字符串。另一种是在结构中为名称设置数组,并复制字符串。
请注意strdup
不是标准的C函数,但几乎所有系统都有它。另请注意,它将使用malloc
动态分配内存,因此您需要在free
节点之前free
字符串。