c++ - 为什么这个 ifstream 无法将平面对象读入内存?

标签 c++ oop serialization io ifstream

我有一个平面对象定义如下:

class Student
{
public:
    char name[20];
    long num;

public:
    Student(char *name, long num) : num(num)
    {
        strcpy(this->name, name);
    }

    Student() : num(0)
    {
        strcpy(this->name, "");
    }

    void Serialize(ostream&);
    static Student Deserialize(istream&);
};

在主函数中,我创建了一个这样的对象:Student stu("Hello", -1);然后继续将其序列化为一个文件,然后我在 ifstream 中再次打开该文件.两个流都以二进制模式打开。但是,当我尝试检索反序列化对象中的值时,只有名称是正确的,而数字似乎是随机内存垃圾。此外,ifstream存在并设置了失败位,表明出现了问题。下面是反序列化函数的实现:

Student Student::Deserialize(istream &in)
{
    cout << "Starting deserialization (short)" << endl;
    Student stu;

    if (in.good())
    {
        in.read((char *) &stu, sizeof(Student));
    }
    if (in.fail())
    {
        cerr << "Deserialization failed (short)" << endl;
    }
    return stu;
}

我在这件事上称它为:

ifstream ifile("test.bin", ios::binary);
Student stu2;
stu2 = Student::Deserialize(ifile);

编辑

根据要求,这是序列化函数:

void Student::Serialize(ostream &out)
{
    cout << "Serializing student: " << name << endl;
    if (out.good())
    {
        out.write(name, 20); // name
        out.write((char *) &num, sizeof(num)); // num
        out.flush();
    }
    if (out.bad())
    {
        cerr << "Read error on IO operation" << endl;
    }
    if (out.fail())
    {
        cerr << "Serialization failed" << endl;
    }
    else
    {
        cout << "Serialization succesful" << endl;
    }
}

以及文件内容test.binod 给出的十六进制形式, 然后是反序列化对象的内容:

0000000 654a 6e61 4c2d 6375 4e00 0072 0000 0000
0000020 8150 d609 ffff ffff ffff ffff
0000034

Name: Hello
Number: 4294967295

最佳答案

您的Serialize() 函数正在保存Student 的成员。您的 Deserialize() 函数试图一次读取整个 Student。由于多种原因,这行不通,数据对齐只是其中之一(即使您完美地处理了其他一切。)

因此,让这两种方法以完全相同的方式工作:要么逐个成员序列化和反序列化成员,要么序列化和反序列化整个类。

关于c++ - 为什么这个 ifstream 无法将平面对象读入内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34100359/

相关文章:

C#如何继承默认构造函数

c++ - 如何用msgpack-c打包多键映射

c# - 在使用 Json.NET 序列化期间向所有类添加自定义类型名称

c++ - 将 SampleGrabber 与 CaptureGraphBuilder2 结合使用时出现问题

c++ - MSVC UTF8 字符串编码使用不正确的代码点

c# - 类既扩展了抽象类又实现了接口(interface)

java - 是否可以在另一个类中重写一个类的方法?

c++ - boost 串行器 : getting warning C4308 unless using virtual class destructor

c++ - 如何在MFC中设置对话框的默认大小(以像素为单位)

c++ - 转到 http : no such file after sending image from Qt client