我在從存儲文件讀取序列化對象時遇到問題。 (請參閱下面的代碼)。C++:自定義對象序列化/反序列化失敗
序列化過程「有效」(雖然可能寫得很差),並且因爲無法知道用戶是否傳遞了std :: ios :: binary標誌或者沒有選擇不使用格式化用whitespace
輸出。 (這也節省內存爲大量像素數據的潛在對我並沒有失去。)
我第一次嘗試看起來一樣的樣品,但int
小號是unsigned char
s的意圖位壓縮的下半部分和上半部分放入char
s中,然後重新組裝它們。
目前我可以讀取所有的數據到一個文件中,但是當我試圖讀取第一片非校驗和數據,要麼返回0(與char
S中的嘗試的情況下)或垃圾的(在的情況下與int
S上的嘗試)
連載:
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;
}
什麼具有格式標誌'std :: ios_base :: binary'做與不做格式化I/O?該標誌只是決定如何處理行結束符:當以非二進制模式寫入'\ n'時,它可能被替換(例如通過CR/LF),而在二進制模式下它不會被替換。類似地,當讀取結束行序列被非二進制模式中的單個'\ n'替換或以二進制模式保持原樣時。爲防止用戶使用或不使用此標誌打開,您應該使用格式化的I/O! – 2012-01-08 23:40:01
查看代碼:在不插入空格的情況下編寫多個值將導致'std :: istream'讀取一個較大的值而不是多個較小的值。可能這個大值會導致溢出錯誤,從而導致數據流進入不良狀態。這就是你的問題開始的地方...... – 2012-01-08 23:42:33
@DietmarKühl:做出答案並且我會接受它。 :d – Casey 2012-01-09 03:04:02