1

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

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

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

目前我可以将所有数据读取到文件中,但是当我尝试读取第一条非校验和数据时,它要么返回 0(在尝试使用chars 的情况下)或垃圾(在尝试使用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;
}
4

2 回答 2

0

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

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

和 center 和 frameDimensions 类似

于 2012-01-08T23:47:57.003 回答
-1

我会使用bitfileds。只是不要忘记在#pragma pack(1)之前和#pragma pack()之后添加。由于计算机只能寻址字节,因此您必须确保您的位域是8. 此外,位的打包取决于编译器/机器,因此您的阅读器应该使用相同的编译器进行编译。然后只需使用例如:

somefilestream.write(&some_bitfield_struct, sizeof(some_bitfield_struct));
于 2012-01-09T00:27:25.173 回答