VC MFC 持久化与序列化

一些特定的点集情况下,出现特殊的情况,为了以后能够重现这种情况,需要对点集进行永久的保存。

首先把点集存为文件的过程,是持久化的过程。然后把文件加载进来的过程是序列化的过程对于其他由MFC类来表示的非原始数据类型如CString对象等的序列化则可以通过对“<<”和“>>”运算符的重载来解决,可以用此方式进行序列化的MFC类和结构有CString,CTime,CTimeSpan,COleVariant,COleCurreny,COleDateTime,COleDateTimeSpan,CSize,CPoint,CRect,SIZE,POINT和RECT等


void serial(int n, DPOINT *p,TCHAR* path, string name)
{
	//TCHAR szTchar[18] = "TCHAR";
	CString str;
	str.Format(_T("%s"),path);
	std::string strStl;

	strStl=str.GetBuffer(0);

	string dir= strStl;
	dir += name;


	CString strpath;

	//std::string strStl="test";

	strpath=dir.c_str();


	//CString str = "sssssss";
	TCHAR m_buf[1000];
	_tcscpy(m_buf, strpath);
	CFile file(m_buf,CFile::modeCreate | CFile::modeWrite); //定义一个文件流对象
	CArchive ar (&file, CArchive::store);
	int npoint = n;
	ar<<npoint;
	for (int i = 0; i < npoint; i++)
	{
		ar<<p[i].x<<p[i].y;
	}
	ar.Flush();
	//读完毕,关闭文件流
	ar.Close();
	file.Close();

}

加载的过程

void load(Point2d *p,int *n,TCHAR* path, string name)
{

	CString str;
	str.Format(_T("%s"),path);
	std::string strStl;

	strStl=str.GetBuffer(0);

	string dir= strStl;
	dir += name;


	CString strpath;

	
	strpath=dir.c_str();

	TCHAR m_buf[1000];
	_tcscpy(m_buf, strpath);

	CFile file(m_buf,CFile::modeRead); //定义一个文件流对象
	// 从档案装载VarA、VarB
	CArchive ar2 (&file, CArchive::load);
	int temp=0;
	ar2 >>temp;
	*n=temp;
	for (int i = 0; i < *n; i++)
	{
		ar2 >>p[i].t>> p[i].value; 

	}


	ar2.Flush();
	//读完毕,关闭文件流
	ar2.Close();
	file.Close();
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值