CFile file("result.txt ",CFile::modeCreate|CFile::modeRead); ; 
char *pBuf;
DWORD dwFileLen;
dwFileLen=file.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
file.Read(pBuf,dwFileLen);
file.Close();
我在MFC里又加了一个CFile类 如上代码是读取数据的 可是每次运行到这的时候就会把result里的数据清空了,还有我想result。txt里写数据时如果用的是fwrite(recvBuf,strlen(recvBuf),1,fp);就可以写进去,但是如果用
CFile file("result.txt",CFile::modeCreate|CFile::modeWrite);
    CTime t = CTime::GetCurrentTime();
CString str1=t.Format("%y.%m.%d %H:%M:%S:");
file.Write(str1,strlen(str1));
   file.Write(recvBuf,strlen(recvBuf));
         file.Close();第一次写进去的数据就会被第二次写进去的覆盖掉
这是为什么呢

解决方案 »

  1.   

    CFile::modeCreate 楼主仔细查看一下这些模式的说明吧, CFile::modeCreate会销毁原来文件中的内容.你需要的应该是modeReadWrite
      

  2.   

    CFile::modeCreate   Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.
      

  3.   

    楼主注意用SeekToEnd,又重复帖
      

  4.   

    CFile file("result.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
    file.SeekToEnd();
    file.Write(...);