一个关于CStdioFile的问题

zilin82 2004-10-04 01:28:14
保存时总是有问题
下面是部分代码:
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
...全文
181 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_bluebird 2004-12-18
  • 打赏
  • 举报
回复
学习
zilin82 2004-10-05
  • 打赏
  • 举报
回复
为什么把:
for(int i = 0;i <nCount;i++)
{
int nLenth = m_wndEdit.LineLength(i);
m_wndEdit.GetLine(i,str.GetBuffer(nLenth));
//改为m_wndEdit.GetLine(i,str.GetBuffer(nLenth),512);就可以
//但是改为m_wndEdit.GetLine(i,str.GetBuffer(nLenth),nLenth);就不行
//为什么?????????????
file.WriteString(str);
str.ReleaseBuffer(i);
}
bResult = TRUE;
}
zilin82 2004-10-05
  • 打赏
  • 举报
回复
好像qfly的方法可行,谢谢了
zilin82 2004-10-05
  • 打赏
  • 举报
回复
我把那一段改为了:
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;
}
但还是不行
zhaogaojian 2004-10-05
  • 打赏
  • 举报
回复
qfly()的方法比较好
zhaogaojian 2004-10-05
  • 打赏
  • 举报
回复
BOOL CChildView::SaveFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
char p[256];//256是最大的行字符数,根据实际需要,你自己改变
try
{
CStdioFile file(pszFile,CFile::modeCreate|CFile::modeWrite);
//file.WriteString(";;;");
int nCount = m_wndEdit.GetLineCount();
CString str;
int i = 0;
while(nCount)
{
m_wndEdit.GetLine(i++,p,256);
//str.ReleaseBuffer();
str=p;
//AfxMessageBox(str);
file.WriteString(str);
str.Empty();

nCount--;
}
file.Close();
bResult = TRUE;
}
catch(CFileException e)
{
e.ReportError();
e.Delete();
}

return bResult;
}
qfly 2004-10-05
  • 打赏
  • 举报
回复
//要另指定长度,因为GetBuffer()的第一个字未指定长度。如:
int nLenth = m_wndEdit.GetLine(i,str.GetBuffer(512), 512);
file.WriteString(str.Left(nLenth));
str.ReleaseBuffer();

lovenoend 2004-10-04
  • 打赏
  • 举报
回复
m_wndEdit.GetLine(i++,str.GetBuffer(2));
// 这里出问题了,GetBuffer()参数代表预保留的Buffer大小,
// 你用2,则就只保留 2 BYTE的Buffer,显然不够大
// 而且你还没有恢复Buffer
改为:
m_wndEdit.GetLine(i++,str.GetBuffer(2048));
str.ReleaseBuffer(); // 函数名可能不正确

file.WriteString(str.GetBuffer(2));
改为:
file.WriteString(str);
zhaogaojian 2004-10-04
  • 打赏
  • 举报
回复
放你信箱里别人进不了
我的信箱step.2000@163.com
发过来让我看看
nwpulipeng 2004-10-04
  • 打赏
  • 举报
回复
帮顶混分
zilin82 2004-10-04
  • 打赏
  • 举报
回复
楼上的还是不行
我把整个程序放到我的邮箱上去了,希望各位可以帮忙调试一下,xiaozilin_82@163.com
Kudeet 2004-10-04
  • 打赏
  • 举报
回复
按上面的方法试试,不行的话,愿意就发到 lai_yiling@163.com 我看看
如果发了留个消息
Kudeet 2004-10-04
  • 打赏
  • 举报
回复
也就是你取是字符串是没有问题,是吧!
Kudeet 2004-10-04
  • 打赏
  • 举报
回复
Example

// The pointer to my rich edit control.
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的例子
zilin82 2004-10-04
  • 打赏
  • 举报
回复
楼上的还是不行,我的问题是没有保存上
huwei001982 2004-10-04
  • 打赏
  • 举报
回复
修改下面的
m_wndEdit.GetLine(i++,str.GetBuffer(2));
file.WriteString(str.GetBuffer(2));
str.Empty();
--------------------------------------

m_wndEdit.GetLine(i++,str.GetBuffer(2));
str.ReleaseBuffer();
file.WriteString(str);
str.Empty();
zuozl 2004-10-04
  • 打赏
  • 举报
回复
str.GetBuffer(2) ?这个有问题吧。。。

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧