一些特定的点集情况下,出现特殊的情况,为了以后能够重现这种情况,需要对点集进行永久的保存。
首先把点集存为文件的过程,是持久化的过程。然后把文件加载进来的过程是序列化的过程对于其他由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();
}