You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.2 KiB
C++
73 lines
1.2 KiB
C++
#pragma once
|
|
#include "stdafx.h"
|
|
//读取数据时有异常处理.在增减参数时,可兼容不同版本,(必须在末尾进行)
|
|
//用法:
|
|
/*
|
|
void WRdata(bool bWrite)
|
|
{
|
|
CSmartArchive sar(L".\\param.ini", bWrite);
|
|
sar + name;
|
|
sar + age;
|
|
sar + Weight;
|
|
}
|
|
*/
|
|
class CSmartArchive
|
|
{
|
|
public:
|
|
CSmartArchive(CString filePath, bool bStore)
|
|
{
|
|
pArchive =NULL;
|
|
if (bStore)
|
|
{
|
|
m_Mode = CArchive::store;
|
|
if (m_file.Open(filePath, CFile::modeCreate | CFile::modeWrite))
|
|
{
|
|
pArchive = new CArchive(&m_file, CArchive::store);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_Mode = CArchive::load;
|
|
if (m_file.Open(filePath, CFile::modeRead))
|
|
{
|
|
pArchive = new CArchive(&m_file, CArchive::load);
|
|
}
|
|
}
|
|
}
|
|
|
|
~CSmartArchive()
|
|
{
|
|
if (pArchive)
|
|
{
|
|
pArchive->Close();
|
|
m_file.Close();
|
|
delete pArchive;
|
|
pArchive = NULL;
|
|
}
|
|
};
|
|
template <typename T> void operator + (T & par)
|
|
{
|
|
if (pArchive)
|
|
{
|
|
if (m_Mode != CArchive::load)
|
|
{
|
|
(*pArchive) << par;
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
(*pArchive) >> par;
|
|
}
|
|
catch (...) { /*AfxMessageBox(L"本次欲读取数据个数,大于上次写入个数!"); */};
|
|
}
|
|
}
|
|
};
|
|
|
|
private:
|
|
CArchive::Mode m_Mode;
|
|
CArchive * pArchive=NULL;
|
|
CFile m_file;
|
|
};
|
|
|