开发中需要对CStringArray进行保存操作,涉及到序列化,特总结一下:
//写
CStringArray saTmp1;
CStringArray saTmp2
saTmp1.AddString(_T("str1"));
saTmp2.AddString(_T("str2"));
CFile file;
file.Open(_T("C:\\mycfg.dat"), CFile::typeBinary | CFile::modeCreate | CFile::modeWrite);
CArchive(&file, CArchive::store);
saTmp1.Serialize(ar);
saTmp2.SErialize(ar);
ar.Close();
file.Close(); //读
CFile file;
file.Open(_T("C:\\mycfg.dat"), CFile::typeBinary | CFile::modeRead);
CArchive ar(&file, CArchive::load);
CStringArray saTmp1;
CStringArray saTmp2;
saTmp1.Serialize(ar);
saTmp2.Serialize(ar);
ar.close();
file.close();
for(int i = 0; i <saTmp1.GetCount(); i++)
{
AfxMessageBox(saTmp1.GetAt(i));
}
for(int j = 0; j <saTmp2.GetCount(); j++)
{
AfxMessageBox(saTmp2.GetAt(j));
}
备注:上述代码最好放在try{}catch{}中,并判断每一步的返回值。