VC保存文件的参考代码
【打印文章】
void CDiaryDlg::OnButtonSave()
{
// TODO: Add your control notification handler code here
// 这种用bSave的方式来判断是否保存过,这样不好,待考虑。
// if (!bSave)
{
int iYear = m_cMonthView.GetYear();
int iMonth = m_cMonthView.GetMonth();
int iDay = m_cMonthView.GetDay();
CString sFileName;
sFileName.Format("%d-%d-%d.txt", iYear, iMonth, iDay);
CFileDialog dlg(FALSE, "txt", sFileName,
OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,
"文本文件(*.txt)|*.txt||", this);
dlg.m_ofn.lpstrTitle = _T("保存日记文件");
if (dlg.DoModal() == IDOK)
{
// 实现存盘
CString fileName = dlg.GetPathName();
CFile file(fileName, CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
CArchive ar(&file, CArchive::store|CArchive::bNoFlushOnDelete);
UpdateData(TRUE);
ar<<m_sComment;
ar.Close();
bSave = TRUE;
m_sFileName = fileName;
}
}
/*
else
{
CFile file(m_sFileName, CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
CArchive ar(&file, CArchive::store|CArchive::bNoFlushOnDelete);
UpdateData(TRUE);
ar<<m_sComment;
ar.Close();
}
*/
}
{
// TODO: Add your control notification handler code here
// 这种用bSave的方式来判断是否保存过,这样不好,待考虑。
// if (!bSave)
{
int iYear = m_cMonthView.GetYear();
int iMonth = m_cMonthView.GetMonth();
int iDay = m_cMonthView.GetDay();
CString sFileName;
sFileName.Format("%d-%d-%d.txt", iYear, iMonth, iDay);
CFileDialog dlg(FALSE, "txt", sFileName,
OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,
"文本文件(*.txt)|*.txt||", this);
dlg.m_ofn.lpstrTitle = _T("保存日记文件");
if (dlg.DoModal() == IDOK)
{
// 实现存盘
CString fileName = dlg.GetPathName();
CFile file(fileName, CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
CArchive ar(&file, CArchive::store|CArchive::bNoFlushOnDelete);
UpdateData(TRUE);
ar<<m_sComment;
ar.Close();
bSave = TRUE;
m_sFileName = fileName;
}
}
/*
else
{
CFile file(m_sFileName, CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
CArchive ar(&file, CArchive::store|CArchive::bNoFlushOnDelete);
UpdateData(TRUE);
ar<<m_sComment;
ar.Close();
}
*/
}
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】