C:fgets用于构建char *链表的用法 [英] C: fgets usage for building a linked list of char*

查看:67
本文介绍了C:fgets用于构建char *链表的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错误地使用了 fgets()吗?

我正在尝试构建一个字符串链接列表( char * ),将每个新行添加到LL的末尾.我正在从文件中读取这些行,但是由于某些原因,只有在 while 循环内使用 fgets()时,每一行才会被当前正在处理的行覆盖,但是add函数似乎可以正确接收每一行.

I am trying to build a linked list of strings (char *) adding each new line to the end of the LL. I am reading these lines from a file, but for some reason every line gets overwritten by the current line being processed, only when using fgets() inside the while loop, but the add function seems to be receiving each line correctly.

如果我分别在 main()中添加行,则不会有问题.

If I add lines individually in main() there are no problems.

这是一个示例输入文件:

Here is a sample input file:

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;
}

有人可以向我解释发生了什么吗?我把头撞在墙上已经太久了.我已经尝试使用一些注释的打印语句,但调试失败.

Could someone please explain to me what is happening? I have been banging my head against a wall for too long. There are already some commented print statements I have been attempting to use, unsuccessfully for debugging.

推荐答案

您的问题出在add()方法中.它一直向列表添加相同的缓冲区指针.您需要将列表中的缓冲区复制到新分配的空间,即.node-> line也需要被分配,并将newLine复制到其中.不要忘了分配(strlen(newLine)+1).

Your problem is in the add() method. It keeps adding the same buffer pointer to the list. You need to copy the buffer in your list to newly allocated space, ie. node->line needs to be malloced, too, and the newLine copied into it. Don't forget to malloc (strlen (newLine) + 1).

这篇关于C:fgets用于构建char *链表的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆