void CCSerializeDlg::OnStore()
{
TRY
{
CFile file(_T("Point.txt"), CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate|CFile::shareExclusive);
CArchive arLoad(&file, CArchive::load);
file.SeekToBegin();
int nCount = 0;
if (file.GetLength() != 0)
arLoad>>nCount;
arLoad.Close();
CArchive arStore(&file, CArchive::store);
nCount += 1;
arStore<<nCount;
file.SeekToEnd();
CLine line(CPoint(11, 11), CPoint(99, 99), RGB(255, 0, 0)); line.Serialize(arStore);
arStore.Close();
file.Close();
}
CATCH (CFileException, e)
{
e->Delete();
}
END_CATCH
}这段代码执行一次后, 以后再执行为什么nCount的值一直是1不增加?
执行 nCount += 1;
nCount ==1;
除了TRY 系统就释放了变量nCount;
如果想让nCount 再次执行的时候自动+1 则需定义为全局变量 或者静态局部变量
在int nCount = 0;前加上static