社区
C++ 语言
帖子详情
C++ Standard的串行化问题
piethy
2003-10-17 01:27:18
如何在C++ Standard中实现类的串行化,不依赖MFC或者其他系统相关的类库
请提供示例源代码
...全文
90
3
打赏
收藏
C++ Standard的串行化问题
如何在C++ Standard中实现类的串行化,不依赖MFC或者其他系统相关的类库 请提供示例源代码
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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;
};
PHP
串行化
与JSON
串行化
的提出,有效的解决了对象的保存和传输的
问题
,举例来说,我在JavaScript中建立了一个对象,我现在想将这个对象保存到服务器端的数据库中,那么我如何进行操作呢,这个时候往往就用到了对象的
串行化
。...
Visual
C++
2005入门经典--源代码及课后练习答案
2.9.3
C++
/CLI特有的功能——格式化输出 92 2.9.4
C++
/CLI的键盘输入 94 2.9.5 使用safe_cast 95 2.9.6
C++
/CLI枚举 96 2.10 小结 98 2.11 练习题 100 第3章 判断和循环 101 3.1 比较数据值 101 ...
C++
Standard
Library简介
C++
Standard
Library是
C++
语言标准库,由
C++
官方提供并定义了一系列标准的类型、函数和模板(包括输入/输出、字符串处理、数学计算、容器、算法等)。
C++
Standard
Library是
C++
语言的基础,并且是所有
C++
开发人员...
【转】PHP
串行化
与JSON
总目录 What 、Why、How <br />What Why How PHP
串行化
语法 PHP
串行化
实例 在JavaScript中
串行化
为JSON—使用json2.js 在JavaScript中
串行化
为JSON—使用...
php
串行化
与json(转)--很不错的文章
总目录 What 、Why、How What Why How PHP
串行化
语法 PHP
串行化
实例 在JavaScript中
串行化
为JSON—使用json2.js 在JavaScript中
串行化
为JSON—使用prototype.js PHP与JSON ...
C++ 语言
64,680
社区成员
250,490
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章