下你所需,载你所想!
汇集开发技术源码资料

c++使用msgpack序列化数据

:848.855KB :1 :2022-10-10 15:33:06

部分简介

c++使用msgpack序列化数据如果开发者对于本文件有需要的可以参考。
```
#include "stdafx.h"
#include
#include
#include
#include
#include "fstream"
``
class CRecordAction
{
public:
CRecordAction(WORD wMainId, WORD wCmdId, WORD wId, void* data, int len, WORD dwTimeSpan);
~CRecordAction();
//void SetManageSink(CRecordActionManage* pSink) {
// m_ManageSink = pSink;
//}
public:
void Clear();
void DoCmd();
private:
//CRecordActionManage* m_ManageSink;
WORD m_wChairId; //座位ID
DWORD m_dwUserId; //用户ID
BYTE m_cbCard; //操作的牌
WORD m_wCmdMainId;
WORD m_wCmdSubId; //指令ID
// void* m_pVoidData;
int m_nLen;
public:
MSGPACK_DEFINE(m_wCmdMainId, m_wChairId, m_nLen);
};
CRecordAction::CRecordAction(WORD wMainId, WORD wCmdId, WORD wId, void* data, int len, WORD dwTimeSpan) :
m_wCmdMainId(wMainId),
m_wCmdSubId(wCmdId),
m_wChairId(wId),
// m_pVoidData(data),
m_nLen(len)
// m_dwSpan(dwTimeSpan)
{
;
/* if (len != 0) {
m_pVoidData = new char[len];
memcpy_s(m_pVoidData, len, data, len);
}
m_ManageSink = NULL;*/
}
CRecordAction::~CRecordAction()
{
;
}
int main()
{
CRecordAction t1(1,2,3,NULL,4,5),t2(6,7,8,NULL,9,10);
msgpack::sbuffer buffer_;
msgpack::packer pack_(&buffer_);
pack_.pack(t1);//序列化三个对象
pack_.pack(t2);
pack_.pack(t2);
/*生成二进制文件本地保存*/
std::ofstream of("1.dat",std::ios::binary);
of.write(buffer_.data(),buffer_.size());
of.close();
/*文件二进制读取*/
std::ifstream is("1.dat",std::ios::binary);
is.seekg (0, is.end);
int n = is.tellg();
is.seekg (0, is.beg);
msgpack::unpacker unpack_;
unpack_.reserve_buffer(n);
is.read(unpack_.buffer(), n);
unpack_.buffer_consumed(n);
msgpack::unpacked result_;
while (unpack_.next(result_))
{
std::cout << result_.get() << std::endl;
}
system("pause");
return 0;
}

热门推荐

相关文章