每执行一次写入,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("输入成功");
}

解决方案 »

  1.   

    为什么会清空?用CArchive那应该怎么写呢?
      

  2.   

    fread.Open("Debug\\rec\\zz.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite)
      

  3.   

    换成你的代码也还是不行啊。
    我已经手动创建了zz.txt文件了。
    fread.Open("Debug\\rec\\zz.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite)
    fread.Open("Debug\\rec\\zz.txt",CFile::modeWrite
    这两句代码的作用应该就一样了吧。
      

  4.   

    你在看看吧,我做了,都可以.
    你应该错在你的文件,文字太短了.
    你所写进文件的strresult会覆盖原件的.
    例:   原文件:1234567890
        strresult:AA
    则现在的文件就是:AA234567890(前面还有不清楚是什么)
    AA覆盖了12
      

  5.   

    哦,明白了,CArchive类每次写入时都会覆盖原来写入的内容。有没有什么方法让它不覆盖原来的内容呢?
      

  6.   

    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);
         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 ?
      

  7.   

    嗯 十楼这样可以写入,我用用CArchive试试,发现加上SeekToEnd()之后,再用CArrchive读取到CString的时候又会出现类型不匹配。
      

  8.   

    直接用Cfile写就好了你要是要一直向文件写东西的话开始写之前 打开文件 然后一直写,直到全部写完再关闭文件不要频繁的打开关闭像你这样根本没体现CArchive的作用
      

  9.   

    嗯 还是用CFile吧 :-)
    用CArchive有什么好处呢?感觉它重载的操作符“<<”">>"挺方便的
      

  10.   

    把你的代码发给我吧(带文件和内容),写文件的时候有点东东,它比我写入的多了点东西,我也不知道是什么.
    就是strresult是"1234时",写入文件的不是只有"1234"
    [email protected]