【问题标题】:Why is this load function only grabbing the first thing in the file?为什么这个加载函数只抓取文件中的第一件事?
【发布时间】:2018-09-26 23:04:13
【问题描述】:

我知道这与 eof 有关,但我不知道流的确切工作原理,如果有人能告诉我发生了什么,我会更好地理解。

假设我有 3 个数字 {1, 2, 3} load 函数将变量放入节点中,但是当我打印所有节点时,只会打印 1 个。

void load() {
    ifstream fload;
    node *n = new node;
    node *temp = new node;
    fload.open("DoubleList.dat");
    if (fload) {
        fload >> n->data;
        n->next = NULL;
        n->prev = NULL;
        head = n;
        tail = n;
        curr = n;
        while (!fload.eof()) {
            fload >> temp->data;
            temp->next = NULL;
            temp->prev = curr;
            curr = temp;
            tail = temp;

        }
    }
}

【问题讨论】:

  • 您似乎从未将节点的next 设置为NULL。该列表将始终显示为最多 1 个元素。
  • 你应该阅读this question
  • 我认为你是对的,可能是一个简单的错误。 :( 已经看了好几个小时了,哈哈,其实它不需要指向任何我不认为的东西。
  • 您只分配了 2 个节点,所以我不确定您打算如何存储 3 个值。循环内应该有一个分配
  • 当您发现自己在寻找时间时,请停下来。查阅您的设计文档,看看哪里出了问题。如果你没有设计,你为什么要写代码?如果你不知道你在写什么以及为什么,你几乎总是在浪费你的时间。如果您确实编写了代码,如果没有明确列出可测试指标的程序目标,您如何知道它是否正常工作?

标签: c++ c++11 eof


【解决方案1】:

您只分配了 2 个nodes。如果文件的值少于 2 个,则会泄漏内存。如果文件有超过 2 个值,您不会为 每个 值分配一个新的 node

也不要依赖eof()。让operator>> 告诉你它是否成功读取了一个值。

试试类似的方法:

void load() {
    // TODO: make sure the list is freed and head/tail are null before continuing!

    ifstream fload;
    fload.open("DoubleList.dat");

    node **n = &head;
    T data; // <-- use whatever your actual node data type is...

    while (fload >> data) {
        *n = new node;
        (*n)->data = data;
        (*n)->next = NULL;
        (*n)->prev = tail;
        tail = *n;
        n = &(tail->next);
    }
}

【讨论】:

  • 两个关于node **n = &amp;head; 的注释供未来读者参考。 head 只不过是一个无节点的next。通过抽象出细微的差异,您可以对headnext 使用相同的代码,从而消除许多几乎相同的代码。第二个n 包含指向最后一个节点的next(或head)的指针。这意味着您不必跟踪最后一个节点来更新其next。这使您无需额外的变量和簿记代码。
【解决方案2】:
#include <fstream>

using namespace std;

struct node
{
    node *next;
    node *prev;
    double data;

    node(node *next, node *prev, double data)   // give node a constructor to not 
    : next{ next }, prev{ prev }, data{ data }  // clutter code with assignments
    {}
};

// assuming load is some member function of a list that has a head and a tail
void load() {
    ifstream fload{ "DoubleList.dat" };  // use constructor to open file
    if (!fload.is_open())
        return;  // early exit if file could not be opened

    node *root = nullptr;
    node *curr = root;

    double value;
    while (fload >> value)  // as long as doubles can be successfully extracted
    {
        if (!root) {  // if our list doesn't have a root yet
            curr = root = new node(nullptr, nullptr, value);
            head = tail = curr;
            continue;
        }

        curr->next = new node(nullptr, curr, value);  // construct the next node
        tail = curr = curr->next;  // and make it the current one.
    }
}

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多