一个关于CStdioFile的问题
保存时总是有问题
下面是部分代码:
void CChildView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,_T("txt"),_T("*.txt"),
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY,m_szFilters);
if(dlg.DoModal() == IDOK)
{
if(LoadFile(dlg.GetPathName()))
m_szPathName = dlg.GetPathName();
m_wndEdit.SetFocus();
}
}
BOOL CChildView::LoadFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
try{
CStdioFile file(pszFile,CFile::modeRead);
OnFileNew();
CString str;
CString str1(_T(""));
while(file.ReadString(str))
{
str1 += str;
str1 += "\n";
}
str1.Delete(str1.GetLength());
m_wndEdit.SetWindowText(str1);
bResult = TRUE;
}
catch(CFileException* e){
e->ReportError();
e->Delete();
}
return bResult;
}
void CChildView::OnFileSave()
{
// TODO: Add your command handler code here
if(!m_szPathName.IsEmpty())
SaveFile(m_szPathName);
else
OnFileSaveAs();
}
void CChildView::OnFileSaveAs()
{
// TODO: Add your command handler code here
CFileDialog dlg(FALSE,_T("txt"),_T("*.txt"),
OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,m_szFilters);
if(dlg.DoModal() == IDOK)
if(SaveFile(dlg.GetPathName()))
m_szPathName = dlg.GetPathName();
TRACE(m_szPathName);
}
//////////下面可能是错误所在地
BOOL CChildView::SaveFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
try
{
CStdioFile file(pszFile,CFile::modeCreate|CFile::modeWrite);
int nCount = m_wndEdit.GetLineCount();
CString str;
int i = 0;
while(nCount)
{
m_wndEdit.GetLine(i++,str.GetBuffer(2));
file.WriteString(str.GetBuffer(2));
str.Empty();
nCount--;
}
bResult = TRUE;
}
catch(CFileException e)
{
e.ReportError();
e.Delete();
}
return bResult;
}
如果有兴趣的话,我可以把程序发给你调试一下,留下e_mail,谢谢!!!!!11