gpt4 book ai didi

c++ - 为什么流继续阅读并且不会到达eof?

转载 作者:行者123 更新时间:2023-11-28 06:40:39 26 4
gpt4 key购买 nike

我正在实现一个非常简单的序列化库,用于保存/恢复 user_info 对象,它包含一个 int 和一个 std::string,这是我的代码:

#include <iostream>
#include <sstream>
using namespace std;

struct user_info {
int id;
string name;
};

// for any POD type like int, read sizeof(int) from the stream
template <class stream_t, class T>
void de_serialize(stream_t& stream, T& x) {
stream.read((char*)&x, sizeof(T));
}

// for std::string, read length(int) first, then read string content when length>0
template <class stream_t>
void de_serialize(stream_t& stream, std::string& str) {
int len;
de_serialize(stream, len);
str.resize(len);
char x;
for(int i=0; i<len; i++) {
de_serialize(stream, x);
str[i] = x;
}
}

// read from stream, fill id and name
template <typename stream_t>
void de_serialize(stream_t& ss, user_info& ui) {
de_serialize(ss, ui.id);
de_serialize(ss, ui.name);
}


int main() {
// read from file, but here I use a 8-bytes-content represents the file content
stringstream ifs;
// two int, \x1\x1\x1\x1 for id, \x0\x0\x0\x0 for name
ifs.write("\x1\x1\x1\x1\x0\x0\x0\x0", 8);
while(!ifs.eof()) {
user_info ui;
de_serialize(ifs, ui);
// when first time goes here, the stream should've read 8 bytes and reaches eof,
// then break the while loop, but it doesn't
// so the second time it calls de_serialize, the program crashes
}
}

代码说明了反序列化的部分。 while循环本来应该运行一次,stream到了eof,为什么还不停循环呢?第二个循环导致崩溃。

请引用评论,谢谢。

最佳答案

如果流出现错误情况,eof() 标志永远不会被设置。在 eof() 上循环通常是错误的。我在这里要做的是更改 de_serialize() 函数的返回类型以返回流,然后围绕 de_serialize() 函数重写循环

像这样:

#include <iostream>
#include <sstream>
using namespace std;

struct user_info {
int id;
string name;
};

// for any POD type like int, read sizeof(int) from the stream
template <class stream_t, class T>
stream_t& de_serialize(stream_t& stream, T& x) {
stream.read((char*)&x, sizeof(T));
return stream; // return the stream
}

// for std::string, read length(int) first, then read string content when length>0
template <class stream_t>
stream_t& de_serialize(stream_t& stream, std::string& str) {
int len;
de_serialize(stream, len);
str.resize(len);
char x;
for(int i=0; i<len; i++) {
de_serialize(stream, x);
str[i] = x;
}
return stream; // return the stream
}

// read from stream, fill id and name
template <typename stream_t>
stream_t& de_serialize(stream_t& ss, user_info& ui) {
de_serialize(ss, ui.id);
de_serialize(ss, ui.name);
return ss; // return the stream
}


int main() {
// read from file, but here I use a 8-bytes-content represents the file content
stringstream ifs;
// two int, \x1\x1\x1\x1 for id, \x0\x0\x0\x0 for name
ifs.write("\x1\x1\x1\x1\x0\x0\x0\x0", 8);

user_info ui;
while(de_serialize(ifs, ui)) { // loop on de_serialize()

// Now you know ui was correctly read from the stream

// when first time goes here, the stream should've read 8 bytes and reaches eof,
// then break the while loop, but it doesn't
// so the second time it calls de_serialize, the program crashes
}
}

关于c++ - 为什么流继续阅读并且不会到达eof?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com