我是否错误地使用了fgets()
?C:用于构建char链表的fgets用法*
我正在尝试建立一个链接列表(char *
),将每一行添加到LL的末尾。我正在从一个文件中读取这些行,但由于某种原因,每行都被正在处理的当前行覆盖,只有在while
循环内使用fgets()
时,但add函数似乎正在正确接收每行。
如果我在main()
中单独添加线条,则没有问题。
下面是一个示例输入文件:
input.txt中:
This life, which had been the
tomb of his virtue and of his
honour, is but a walking
shadow; a poor player, that
struts and frets his hour upon
the stage, and then is heard
no more: it is a tale told by an
idiot, full of sound and fury,
signifying nothing.
--William Shakespeare
代码:
#include <stdio.h> //printf, fopen
#include <stdlib.h> //exit, EXIT_FAILURE
#include <string.h> //strlen
struct node {
char *line;
struct node *next;
};
void print(struct node *node);
void add(struct node **head, char *newLine) {
//printf("%s", newLine);
struct node *new_node = (struct node *)malloc(sizeof(struct node));
struct node *curr = *head;
new_node->line = newLine;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
} else {
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = new_node;
}
print(*head);
}
void print(struct node *node) {
printf("\n");
while (node != NULL) {
printf("%s\n", node->line);
node = node->next;
}
}
int main(int argc, char *argv[16]) {
char newLine[81];
struct node *head = NULL;
FILE *fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("ERROR: file open failed");
exit(EXIT_FAILURE);
}
while (fgets(newLine, 81, fp)) {
add(&head, newLine);
}
add(&head, "why");
add(&head, "does");
add(&head, "this");
add(&head, "work??");
fclose(fp);
print(head);
return 0;
}
可能有人请向我解释发生了什么?我一直把头撞在墙上太久。已经有一些我一直在尝试使用的评论打印语句,但没有成功进行调试。
得到一些纸。跟踪调用“add”并绘制链接列表的表示形式。特别要注意'node-> line'指向什么。 –
这种行为有几个kilodupes;( –