CFile file(FileName,CFile::modeCreate|CFile::modeWrite);
    char pbuf[100];
    file.Write( pbuf, 100 );
    
    为何每次写进去文件总把上一次写的覆盖掉。想问的是:如何接着上一次写的继续写下去。望给于帮助,谢谢。

解决方案 »

  1.   

    //CFile使用
        CFile file; //类实例化
        CString msg;
        CString sFile("C:\\Wzd.tmp");  //文件名
        if (!file.Open(sFile, CFile::modeCreate|CFile::modeWrite)) //文件不存在则创建
        {
            msg.Format("Failed to create %s.",sFile);
            AfxMessageBox(msg);
        }    char buffer[255];
        memset(buffer,0,sizeof(buffer));
        sprintf(buffer,"Hello! Can you Help me?");
        file.Write(buffer,sizeof(buffer)); //文件写入    int n=79;
        CString sBuffer;
        sBuffer.Format("%i",n);
        file.Write(sBuffer,sizeof(sBuffer)); //文件写入
        file.Close(); //文件关闭    if (!file.Open(sFile, CFile::modeRead)) //文件打开
        {
            msg.Format("Failed to open %s.",sFile);
            AfxMessageBox(msg) ;
        }    int    nBytes = file.Read(buffer, sizeof(buffer)); //读取文件内容
        AfxMessageBox(buffer);    //file.SeekToBegin();
        //file.SeekToEnd();
        file.Seek(7,CFile::begin); //改变文件读写的当前位置
        nBytes = file.Read(buffer, sizeof(buffer)); //读取文件内容
        AfxMessageBox(buffer);    file.Seek(255,CFile::begin);
        nBytes = file.Read(buffer,sizeof(buffer));
        AfxMessageBox(buffer);
        
        file.Close(); //文件关闭/*    CFileStatus status;
        if(CFile::GetStatus(sFile, status)) //得到文件的属性
        {
            status.m_attribute|= 0x01;
            CFile::SetStatus(sFile,status); //改变文件属性为只读
        }
    //*/    CFile::Remove(sFile); //文件的删除,若文件只读则删除失败
      

  2.   

    不要用CFile::modeCreate,用CFile::modeNoTruncateCFile::modeNoTruncate   Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.
      

  3.   

    lvgame(gameboy):
    但是再次写时被拒绝。
      

  4.   

    alon21(飘一族.Alon):
    是否用  file.Seek(7,CFile::begin); 来定位写的位置?