C++:自定义对象序列化/反序列化失败

标签 c++ serialization file-io binary-serialization

我在从存储的文件中读取序列化对象时遇到问题。 (见下面的代码)。

序列化过程“有效”(尽管可能写得很糟糕),并且因为无法知道用户是否正在传递 std::ios::binary 标志,所以我选择不使用格式化输出使用 空格。 (它还节省了内存,因为我并没有失去大量像素数据的潜力。)

我的第一次尝试看起来与示例相同,但 intunsigned char,目的是将下半部分和上半部分位打包到 中char,然后重新组装它们。

目前我可以将所有数据读取到一个文件中,但是当我尝试读取第一段非校验和数据时,它返回 0(在尝试使用 char 的情况下)或垃圾(在尝试使用 ints 的情况下)

序列化:

std::ostream& operator<<(std::ostream& os, const Sprite& data) {
    int dF = data._dimensions.first;
    int dS = data._dimensions.second;

    int cF = data._center.first;
    int cS = data._center.second;

    int fF = data._frameDimensions.first;
    int fS = data._frameDimensions.second;

    double sF = data._scaleDimensions.first;
    double sS = data._scaleDimensions.second;


    std::string name(*data._file);
    name.shrink_to_fit();
    os << 'S' << 'P' << 'R' << (name.length() + 1) << name.c_str() << dF << dS << cF << cS << fF << fS << sF << sS;
    for(int x = 0; x < data._dimensions.first; ++x) {
        for(int y = 0; y < data._dimensions.second; ++y) {
            int color = getpixel(data._image, x, y);
            os << static_cast<unsigned char>(getr(color)) << static_cast<unsigned char>(getg(color)) << static_cast<unsigned char>(getb(color));
        }
    }
    int tint = data._tint;
    os << static_cast<unsigned char>(getr(tint)) << static_cast<unsigned char>(getg(tint)) << static_cast<unsigned char>(getb(tint));
    os << data._tintIntensity << data._alpha;
    return os;
}

反序列化:

std::istream& operator>>(std::istream& is, Sprite& data) {
    char checksum[3];
    is >> checksum[0] >> checksum[1] >> checksum[2];
    if(checksum[0] != 'S' || checksum[1] != 'P' || checksum[2] != 'R') {
        is.setstate(std::ios::failbit);
        return is;
    }
    int name_length;
    is >> name_length;

    std::string name(name_length, '\0');

    for(int i = 0; i <= name_length; ++i) {
        char current_char = '\0';
        is >> current_char;
        name[i] = current_char;
    }

    int upper = 0;
    int lower = 0;
    is >> upper;
    is >> lower;
    data._dimensions.first = (upper << 8) | lower;

    upper = 0;
    lower = 0;
    is >> upper >> lower;
    data._dimensions.second = ((upper << 8) | lower);

    upper = 0;
    lower = 0;
    is >> upper >> lower;
    data._center.first = ((upper << 8) | lower);

    upper = 0;
    lower = 0;
    is >> upper >> lower;
    data._center.second = ((upper << 8) | lower);

    upper = 0;
    lower = 0;
    is >> upper >> lower;
    data._frameDimensions.first = ((upper << 8) | lower);

    upper = 0;
    lower = 0;
    is >> upper >> lower;
    data._frameDimensions.second = ((upper << 8) | lower);

    double f = 0.0;
    double s = 0.0;
    is >> f >> s;
    data._scaleDimensions.first = f;
    data._scaleDimensions.second = s;

    destroy_bitmap(data._image);
    data._image = NULL;
    data._image = create_bitmap(data._dimensions.first, data._dimensions.second);
    for(int x = 0; x < data._dimensions.first; ++x) {
        for(int y = 0; y < data._dimensions.second; ++y) {
            unsigned char r = 0;
            unsigned char g = 0;
            unsigned char b = 0;
            is >> r >> g >> b;
            int color = ((r << 16)  | (g << 8) | b); //0xRRGGBB
            putpixel(data._image, x, y, color);
        }
    }
    unsigned char rtint = 0;
    unsigned char gtint = 0;
    unsigned char btint = 0;
    is >> rtint >> gtint >> btint;
    data._tint = ((rtint << 16)  | (gtint << 8) | btint); //0xRRGGBB

    is >> data._tintIntensity;
    is >> data._alpha;
    return is;
}

最佳答案

反序列化不应该是这样的:

int upper = 0;
int lower = 0;
is >> upper;
is >> lower;
data._dimensions.first = upper;
data._dimensions.second = lower;

与中心和框架尺寸类似

关于C++:自定义对象序列化/反序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8782348/

相关文章:

c - fwrite 在最后一次迭代中不起作用

java - Java 中的方法在某个位置创建文件,必要时创建目录?

php - 如果在发送 "readfile"的文件时我修改了文件,会不会下载失败?

c++ - 无法在 Eclipse 中运行 SDL 程序,但可以在 Windows 资源管理器中运行

c++ - spirit X3 : Can't compile any example on Ubuntu 16. 04

.net - .NET 序列化的版本兼容性?

json - Moshi 的 Kotlin 代码生成器有什么用?

c++ - 如何有效地将 std::atomic<> 用于非原始类型?

c++ - 变量名后的括号 C++

wcf - System.Runtime.Serialization.DataContractAttribute 在命名空间 System.Runtime.Serialization 中不存在