想复制文件到某目录下,
存在文件则覆盖
我该用哪些函数呢??
望大虾指点下

解决方案 »

  1.   

    BOOL CopyFile(
      LPCTSTR lpExistingFileName,
      LPCTSTR lpNewFileName,
      BOOL bFailIfExists
    );
    bFailIfExists 
    [in] If this parameter is TRUE and the new file specified by lpNewFileName already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds. 
      

  2.   

    //复制文件的函数
    BOOL CommonCopyFile(CString SourceFileName, CString DestFileName)
    {
     CFile sourceFile ;
     CFile destFile ;
     CFileException ex;
     if (!sourceFile.Open((LPCTSTR)SourceFileName,CFile::modeRead | CFile::shareDenyWrite, &ex))
     {
      TCHAR szError[1024];
      ex.GetErrorMessage(szError, 1024);
      CString ErrorMsg = "打开文件:" ;
      ErrorMsg += SourceFileName ;
      ErrorMsg += "失败。\n错误信息为:\n" ;
      ErrorMsg += szError ;
      AfxMessageBox(ErrorMsg);
      return FALSE ;
     }
     else
     {
      if (!destFile.Open((LPCTSTR)DestFileName, CFile::modeWrite | CFile::shareExclusive | CFile::modeCreate, &ex))
      {
       TCHAR szError[1024];
       ex.GetErrorMessage(szError, 1024);
       CString ErrorMsg = "创建文件:" ;
       ErrorMsg += DestFileName ;
       ErrorMsg += "失败。\n错误信息为:\n" ;
       ErrorMsg += szError ;
       AfxMessageBox(ErrorMsg);
       sourceFile.Close();
       return FALSE ;
      }  BYTE buffer[4096];
      DWORD dwRead;
      do
      {
       dwRead = sourceFile.Read(buffer, 4096);
       destFile.Write(buffer, dwRead);
      }
      while (dwRead > 0);   
      destFile.Close();
      sourceFile.Close();
     }
     return TRUE ;
    }