我正在从.txt文件中读取链表以填充以下节点:
typedef struct node {
int data;
char* line;
struct node* next;
} NODE;
代码工作正常,并将文件读入链表。我在初始化每个节点时打印出字符串。但是,每当我调用printList()
函数时,line
字符串始终是垃圾值。示例运行:
./a.out linkedlisttext.txt
data in readFile(): 4
string in readFile(): Hello world
address in readFile(): 0x1326067a0
data in readFile(): 28
string in readFile(): What is up world???
address in readFile(): 0x1326067c0
data in readFile(): 2124
string in readFile(): your mother
address in readFile(): 0x1326067e0
data in readFile(): 85
string in readFile(): more data
address in readFile(): 0x132606800
data in readFile(): 9421
string in readFile(): just a little more data
address in readFile(): 0x132606820
data in readFile(): 992
string in readFile(): are we almost there?
address in readFile(): 0x132606840
data in readFile(): 301
string in readFile(): we did it!
address in readFile(): 0x132606860
DATA: 4 | STRING �g`2
address in printList(): 0x1326067a0
DATA: 28 | STRING �g`2
address in printList(): 0x1326067c0
DATA: 2124 | STRING �g`2
address in printList(): 0x1326067e0
DATA: 85 | STRING h`2
address in printList(): 0x132606800
DATA: 9421 | STRING 0h`2
address in printList(): 0x132606820
DATA: 992 | STRING Ph`2
address in printList(): 0x132606840
DATA: 301 | STRING
address in printList(): 0x132606860
这是linkedlisttext.txt
4, Hello world
28, What is up world???
2124, your mother
85, more data
9421, just a little more data
992, are we almost there?
301, we did it!
这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int data;
char* line;
struct node* next;
} NODE;
struct node* readFile(FILE* fp, NODE* root) {
char line[100];
char* token;
NODE* curr;
NODE* prev;
while(fgets(line, 100, fp) != NULL) {
curr = malloc(sizeof(struct node*));
token = strtok(line, ",");
curr->data = atoi(token);
printf("ndata in readFile(): %dn", curr->data);
token = strtok(NULL, "");
curr->line = (char*) malloc(sizeof(token) + 1);
strcpy(curr->line, token);
printf("string in readFile(): %s", curr->line);
printf("address in readFile(): %pnn", curr->line);
curr->next = NULL;
if(root == NULL) {
root = curr;
} else {
NODE* travel = root;
while(travel->next != NULL) {
travel = travel->next;
}
travel->next = curr;
}
}
return root;
}
void printList(NODE* root) {
struct node* temp = root;
while(temp->next != NULL) {
printf("DATA: %4d | STRING %sn", temp->data, temp->line);
printf("address in printList(): %pn", temp->line);
temp = temp->next;
}
printf("DATA: %4d | STRING %sn", temp->data, temp->line);
printf("address in printList(): %pn", temp->line);
}
int main(int argc, char** argv) {
FILE* fp = fopen(argv[1], "r");
NODE* root = NULL;
if(fp == NULL) {
return 0;
}
root = readFile(fp, root);
printList(root);
}
任何帮助不胜感激,谢谢!
curr->line = (char*) malloc(sizeof(token) + 1);
由于sizeof(token)
总是返回指针的大小(大多数系统为 8 字节), 不是字符串的长度,您没有为line
分配足够的缓冲区。请尝试:
curr->line = malloc(strlen(token) + 1);
作为旁注,您对 main 函数中的行缓冲区和结构成员名称使用相同的名称line
。它们具有单独的命名空间且语法有效,但可能会使将来的维护感到困惑。
这是非常可疑的:
curr = malloc(sizeof(struct node*));
您正在为struct node
指针分配内存,但不为struct node
分配内存。给定指针和整数的内存大小,随后分配给curr->data
可能实际上有效,但随后分配给curr->line
和curr->next
可能会给您带来麻烦。
或者不是。未定义的行为本质上是不可预测的。