将数据序列化到/从 ASCII 文件





2.00/5 (2投票s)
一系列文章,源于我尝试为现有应用程序添加拖放功能
本文是 拖放界面 示例的一部分。
- 序列化 ASCII 数据
- 无模式子对话框
- 无模式兄弟对话框
- 拖放源
- MFC 目标
- TBTextTarget 类
前言
CArchive
对象具有名为 ReadString
和 WriteString
的方法。ReadString
读取完整的行,停止于但不包括回车/换行符对。WriteString
将指定字符串值放入存档,但不包括 CR/LF。ReadString
通过返回零来检测 EOF,这可用于 while 条件语句中。
这是来自 示例应用程序,步骤零:
void CInterfaceDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
for (int i=0; i<m_String.GetUpperBound(); i++)
ar.WriteString(m_Strings[i]+"\n");
}
else
{
m_Strings.RemoveAll();
int idx=0;
CString t;
TRY
{
while(ar.ReadString(t))
{
m_Strings.SetAtGrow(idx, t);
idx++;
}
}
CATCH(CArchiveException, e)
{
#ifdef _DEBUG
TCHAR szCause[255];
CString strFormatted;
e->GetErrorMessage(szCause, 255);
// in real life, it's probably more
// appropriate to read this from
// a string resource so it would be easy to
// localize
strFormatted = _T("CArciveException: ");
strFormatted += szCause;
AfxMessageBox(strFormatted);
#endif //_DEBUG
}
END_CATCH;
UpdateAllViews(NULL);
}
}