每执行一次写入,CArchive都会将以前写入的内容清空。请问哪个地方出了问题?
void CInPut::OnButtonIn()
{
// TODO: Add your control notification handler code here
CFile fread;
if (!fread.Open("Debug\\rec\\zz.txt",CFile::modeWrite))
{
MessageBox("文件未打开");
exit(0);
}
UpdateData(true);
CArchive ar(&fread,CArchive::store );
CString strresult;
strresult.Format("%s%s%s%s","@",m_timu,"#",m_answer);
ar<<strresult;
ar.Flush();
ar.Close();
fread.Close();
MessageBox("输入成功");
}
void CInPut::OnButtonIn()
{
// TODO: Add your control notification handler code here
CFile fread;
if (!fread.Open("Debug\\rec\\zz.txt",CFile::modeWrite))
{
MessageBox("文件未打开");
exit(0);
}
UpdateData(true);
CArchive ar(&fread,CArchive::store );
CString strresult;
strresult.Format("%s%s%s%s","@",m_timu,"#",m_answer);
ar<<strresult;
ar.Flush();
ar.Close();
fread.Close();
MessageBox("输入成功");
}
我已经手动创建了zz.txt文件了。
fread.Open("Debug\\rec\\zz.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite)
fread.Open("Debug\\rec\\zz.txt",CFile::modeWrite
这两句代码的作用应该就一样了吧。
你应该错在你的文件,文字太短了.
你所写进文件的strresult会覆盖原件的.
例: 原文件:1234567890
strresult:AA
则现在的文件就是:AA234567890(前面还有不清楚是什么)
AA覆盖了12
{
// TODO: Add your control notification handler code here
CFile fread;
if (!fread.Open("Debug\\rec\\zz.txt",CFile::modeWrite))
{
MessageBox("文件未打开");
exit(0);
}
UpdateData(true);
fread.SeekToEnd();//加这个 试试
CArchive ar(&fread,CArchive::store );
CString strresult;
strresult.Format("%s%s%s%s","@",m_timu,"#",m_answer);
ar<<strresult;
ar.Flush();
ar.Close();
fread.Close();
MessageBox("输入成功");
}其实你直接用Cfile写就好了,为什么用 CArchive ?
用CArchive有什么好处呢?感觉它重载的操作符“<<”">>"挺方便的
就是strresult是"1234时",写入文件的不是只有"1234"
[email protected]