if(file.Open(path_search,CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary)!=0)
       {
        CFile file1;
TCHAR *p;
        CString filename;
        filename=filestruct.name;
        p=strchr(filestruct.name,'.');
        filename=filename.Mid(0,strlen(filename)-strlen(p));
        CString endfilename;
        endfilename=dirname+filename;
       file.SeekToBegin();
       CString strFind    = "test.bmp";
       CString strReplace = endfilename+".bmp";
       CString strFind1   = "test.mid";
       CString strReplace1 = endfilename+".mid";
       AfxMessageBox(strReplace1);
       UINT nFindCount = 8;       //strFind的长度
       UINT nReplaceCount = 8;    //strReplace的长度
       char *strBuf = new char[nFindCount + 1];    
       memset(strBuf , 0 , nFindCount+1); 
      file1.Open(path1+"file1.tmp",CFile::modeCreate|CFile::modeReadWrite|CFile::typeBinary);//创建临时文件
       while(file.Read(strBuf , nFindCount))
      {
  
      strBuf[nFindCount] = '\0';
       if(!strcmp(strBuf , strFind))
   {
         memcpy(strBuf,strFind,nFindCount); 
   }
        if(!strcmp(strBuf , strFind1))
{
        memcpy(strBuf,strFind1,nFindCount); 
}
   file1.Write(strBuf,nFindCount);//将数据写回临时文件中
       memset(strBuf , 0 , nFindCount+1);
       file.Seek(1, CFile::begin);
   file1.Seek(1, CFile::begin);
       }
     if(CopyFile(file1,file,TRUE))//将原文件内容替换成临时文件内容,这儿如何操作啊?
       delete []strBuf;
       file.Close();
   file1.Close();
 
         }

解决方案 »

  1.   

    if(!strcmp(strBuf , strFind))
    {
             memcpy(strBuf,strFind,nFindCount); 
    }
    if(!strcmp(strBuf , strFind1))
    {
            memcpy(strBuf,strFind1,nFindCount); 
    }
    这里是干什么的?如果strBuf==strFind就把strBuf替换为strFind?既然相同还换什么?
    要退换文件可一把源文件删掉,把修改后的文件重命名就可以了
      

  2.   

    CString name1;
    name1=path1+"tmp.tmp";
        if (!file1.Open(name1,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary, &ex))
     {
             TCHAR szError[1024];
             ex.GetErrorMessage(szError, 1024);
         AfxMessageBox(szError);
     }
     
       if(file.Open(path_search,CFile::modeReadWrite|CFile::typeBinary)!=0)
           {
            
            TCHAR *p;
            CString filename;
            filename=filestruct.name;
            p=strchr(filestruct.name,'.');
            filename=filename.Mid(0,strlen(filename)-strlen(p));
            CString endfilename;
           endfilename=dirname+filename;
           file.SeekToBegin();
           CString strFind    = "test.bmp";
           CString strReplace = endfilename+".bmp";
           CString strFind1   = "test.mid";
           CString strReplace1 = endfilename+".mid";
           UINT nFindCount = 8;       //strFind的长度
           UINT nReplaceCount = 8;    //strReplace的长度
           char *strBuf = new char[nFindCount + 1];    
          memset(strBuf , 0 , nFindCount+1); 
          while(file.Read(strBuf,nFindCount))
           {
          strBuf[nFindCount] = '\0';
         if(!strcmp(strBuf,strFind))
          {
          memcpy(strBuf,strReplace,nFindCount+1); 
          file1.Write(strBuf,strReplace.GetLength());
      }
     else if(!strcmp(strBuf ,strFind1))
     {
     UINT filelen1=strReplace1.GetLength();
     TRACE("%d",filelen1);
     memcpy(strBuf,strReplace1,nFindCount+1); 
         file1.Write(strBuf,strReplace1.GetLength());
     }
     else
         file1.Write(strBuf,nFindCount+1);
         memset(strBuf,0,nFindCount+1); 
         file.Seek(1,CFile::begin); 
     file1.Seek(1,CFile::begin);       }
        
              delete []strBuf;
           
       DeleteFile(path_search);
           file1.Rename(name1,path_search);
       file1.Close();
           file.Close();