从自引用对象的二进制文件在内存中创建链表 C++

发布于 2024-10-26 19:07:04 字数 1031 浏览 1 评论 0原文

我正在尝试从文件读取后填充链接列表。当我运行它时它只是挂起。我知道问题与课程的分配有关(我可能是错的)。 任何指导将不胜感激。

Course course;//this one to read from file
Course* fileCourse = new Course();//this populates linked list
fstream Read("Courses.dat", ios::in | ios::binary);
if(!Read)
    cout << "Error Reading from file Courses.dat\n" << endl;
else
{
    Read.read(reinterpret_cast<char*>(&course), sizeof(Course));
    fileCourse->setNextCourse(&course);//problem here perhaps?
    while(Read && !Read.eof())
    {
        Read.read(reinterpret_cast<char*>(&course), sizeof(Course));
        fileCourse->setNextCourse(&course);
        if(head == NULL)
        {
            head = fileCourse;
        }
        else
        {
            Course* tmp = head;

            tmp = tmp->getNextCourse();

            while(tmp->getNextCourse() != NULL)
            {
                tmp = tmp->getNextCourse();
            }
            tmp->setNextCourse(fileCourse);
        }
    }
}

I am trying to populate a linked list after reading from a file. It simply hangs when i run it. I know the problem is associated with the assignment of &course (I may be wrong).
Any guidance would be much appreciated.

Course course;//this one to read from file
Course* fileCourse = new Course();//this populates linked list
fstream Read("Courses.dat", ios::in | ios::binary);
if(!Read)
    cout << "Error Reading from file Courses.dat\n" << endl;
else
{
    Read.read(reinterpret_cast<char*>(&course), sizeof(Course));
    fileCourse->setNextCourse(&course);//problem here perhaps?
    while(Read && !Read.eof())
    {
        Read.read(reinterpret_cast<char*>(&course), sizeof(Course));
        fileCourse->setNextCourse(&course);
        if(head == NULL)
        {
            head = fileCourse;
        }
        else
        {
            Course* tmp = head;

            tmp = tmp->getNextCourse();

            while(tmp->getNextCourse() != NULL)
            {
                tmp = tmp->getNextCourse();
            }
            tmp->setNextCourse(fileCourse);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-11-02 19:07:04
  1. 这是作业吗?如果是这样,请将其标记为这样。
  2. 要使用 n 项(在本例中为 Course 对象)填充链表,您应该知道 n 对象应分配有 新课程()。您在此代码中分配了多少个对象?
  1. Is this homework? If so, tag it as such.
  2. To populate a linked list with n items (in this case, Course objects) you should know that n objects should be allocated with new Course(). How many objects are you allocating in this code?
忘你却要生生世世 2024-11-02 19:07:04

如果课程不是 POD(普通旧数据),因为它嵌入了成员函数、指针等,您可能无法将其逐字节写入文件,然后将其读入并期望拥有有效的对象。您需要在保存时序列化数据,然后在加载时将其反序列化回有效对象。有时,这是通过重载流运算符 << 来完成的。和>>将类输出/输入到文件中。用于序列化的常见数据格式包括 XML 或 JSON,甚至只是没有任何指针的二进制数据。

您遇到的错误可能与此问题中报告的错误类似:C++ 读取对象来自文件错误

If course is not POD (Plain Old Data) in taht it has member functions, pointers, etc embedded you may not be able to to write it byte for byte for a file and then read it in and expect to have a valid object. You need to serialize the data upon save and then deserialize it back to a valid object on load. This is sometimes done by overloading the stream operators << and >> to output/input the class to a file. Common data formats for serializing include XML or JSON, or even just binary data without any pointers.

The errors you are encountering are probably similar to what is being reported in this question: C++ Reading Objects from File Error

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文