C++ Standard的串行化问题

piethy 2003-10-17 01:27:18
如何在C++ Standard中实现类的串行化,不依赖MFC或者其他系统相关的类库
请提供示例源代码
...全文
90 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bilbo0214 2003-10-17
  • 打赏
  • 举报
回复
可能我的源文件字符集有问题,中文都变成"ÎļþдÈëÊ
了。

VC++6这个编译器有时候让人头疼。
bilbo0214 2003-10-17
  • 打赏
  • 举报
回复
//串行化实现,但是std::ios_base在Dev-C++4.9.7.0下提示未定义的错误,VC++6下可以编译

#if !defined SERSTREAM_H
#define SERSTREAM_H
#include <fstream>
using std::ios_base;

const long TruePattern = 0xfab1fab2;
const long FalsePattern = 0xbad1bad2;

class DeSerializer
{
private:
std::ifstream _stream;
public:
DeSerializer (std::string const & nameFile)
: _stream (nameFile.c_str (), ios_base::in | ios_base::binary)
{
if (!_stream.is_open ()) throw "²»ÄÜ´ò¿ªÎļþ";
};
long GetLong ();
double GetDouble ();
std::string GetString ();
bool GetBool ();
};

class Serializer
{
private:
std::ofstream _stream;
public:
Serializer (std::string const & nameFile)
: _stream (nameFile.c_str (), ios_base::out | ios_base::binary)
{
if (!_stream.is_open ()) throw "²»ÄÜ´ò¿ªÎļþ";
};
void PutLong (long l);
void PutString (std::string const & str);
void PutBool (bool b);
void PutDouble (double d);
};

============================================================================
#include "SerStream.h"

//DeSerializer³ÉÔ±º¯Êý
long DeSerializer::GetLong () {
long l;
if (_stream.eof()) throw "·ÇÕý³£Îļþ½áÊø";
_stream.read (reinterpret_cast<char *> (&l), sizeof (long));
if (_stream.bad()) throw "Îļþ¶ÁÈëʧ°Ü";
return l;
}

double DeSerializer::GetDouble () {
double d;
if (_stream.eof()) throw "·ÇÕý³£Îļþ½áÊø";
_stream.read (reinterpret_cast<char *> (&d), sizeof (double));
if (_stream.bad()) throw "Îļþ¶ÁÈëʧ°Ü";
return d;
}

std::string DeSerializer::GetString () {
long len = GetLong ();
std::string str;
str.resize (len);
_stream.read (&str [0], len);
if (_stream.bad()) throw "Îļþ¶ÁÈëʧ°Ü";
return str;
}

bool DeSerializer::GetBool () {
long b = GetLong ();
if (_stream.bad()) throw "Îļþ¶ÁÈëʧ°Ü";
if (b == TruePattern)
return true;
else if (b == FalsePattern)
return false;
else
throw "data corruption";
}


//Serializer³ÉÔ±º¯Êý
void Serializer::PutLong (long l) {
_stream.write (reinterpret_cast<char *> (&l), sizeof (long));
if (_stream.bad()) throw "ÎļþдÈëʧ°Ü";
}

void Serializer::PutDouble (double d) {
_stream.write (reinterpret_cast<char *> (&d), sizeof (double));
if (_stream.bad()) throw "ÎļþдÈëʧ°Ü";
}

void Serializer::PutString (std::string const & str) {
int len = str.length ();
PutLong (len);
_stream.write (str.data (), len);
if (_stream.bad()) throw "ÎļþдÈëʧ°Ü";
}

void Serializer::PutBool (bool b) {
long l = b? TruePattern: FalsePattern;
PutLong (l);
if (_stream.bad ()) throw "ÎļþдÈëʧ°Ü";
}
bilbo0214 2003-10-17
  • 打赏
  • 举报
回复
//抽象接口定义
#if !defined SERIAL_H
#define SERIAL_H

class Serializer;
class DeSerializer;

class Serializable
{
public:
virtual void Serialize (Serializer & out) const = 0;
virtual void DeSerialize (DeSerializer & in) = 0;
};

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧