保存时总是有问题
下面是部分代码:
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
下面是部分代码:
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
CRichEditCtrl* pmyRichEditCtrl; int i, nLineLength, nLineCount = pmyRichEditCtrl->GetLineCount();
CString strText, strLine;
// Dump every line of text of the rich edit control.
for (i=0;i < nLineCount;i++)
{
nLineLength = pmyRichEditCtrl->LineLength(i);
pmyRichEditCtrl->GetLine(i, strText.GetBuffer(nLineLength));
strText.ReleaseBuffer(nLineLength);
strLine.Format(TEXT("line %d: '%s'\r\n"), i, strText.GetBuffer(0));
afxDump << strLine;
}这是MSDN的例子
// 这里出问题了,GetBuffer()参数代表预保留的Buffer大小,
// 你用2,则就只保留 2 BYTE的Buffer,显然不够大
// 而且你还没有恢复Buffer
改为:
m_wndEdit.GetLine(i++,str.GetBuffer(2048));
str.ReleaseBuffer(); // 函数名可能不正确file.WriteString(str.GetBuffer(2));
改为:
file.WriteString(str);
BOOL CChildView::SaveFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
try
{
CStdioFile file(pszFile,CFile::modeCreate|CFile::modeWrite);
int nCount = m_wndEdit.GetLineCount();
CString str;
for(int i = 0;i <nCount;i++)
{
int nLenth = m_wndEdit.LineLength(i);
m_wndEdit.GetLine(i,str.GetBuffer(nLenth));
file.WriteString(str);
str.ReleaseBuffer(i);
}
bResult = TRUE;
}
catch(CFileException e)
{
e.ReportError();
e.Delete();
}
return bResult;
}
int nLenth = m_wndEdit.GetLine(i,str.GetBuffer(512), 512);
file.WriteString(str.Left(nLenth));
str.ReleaseBuffer();