我要实现文件的拷贝,这么做呢?

解决方案 »

  1.   

    BOOL CopyFile(
      LPCTSTR lpExistingFileName, // name of an existing file
      LPCTSTR lpNewFileName,      // name of new file
      BOOL bFailIfExists          // operation if file exists
    );
    bFailIfExists [in]If this parameter is TRUE and the new file 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.   

    API
    BOOL CopyFile(
      LPCTSTR lpExistingFileName, // name of an existing file
      LPCTSTR lpNewFileName,      // name of new file
      BOOL bFailIfExists          // operation if file exists
    );
      

  3.   

    //文件拷贝函数
    bool C...Dlg::CopyFile(CString lpSourceFile,lpDestFile,int nBufSize)
    {
       CFile Out,In;
       int nFileSize;
       char* pBuf;
       if(!In.Open(lpSourceFile,CFile::modeRead))
       {
           AfxMessageBox("打开源文件失败");
           return false;
       }
       if(!Out.Open(lpDestFile,CFile::modeWrite | CFile::modeCreate)) 
       {
           AfxMessageBox("创建目标文件失败");
           return false;
       }
       lpBuf=new char[nBufSize];
       if(lpBuf==NULL)
       {
           AfxMessageBox("缓冲分配失败");
           return false;
       }
       CFileStatus rStatus;
       In.GetStatus(lpSourceFile,rStatus);//获取源文件属性
       nFileSize=In.GetLength();
       while(nFileSize>0)//开始拷贝
       {
           int nSize=nBufSize;
           if(nSize>nFileSize) nSize=nFileSize;
           try{//读取文件
              In.Read(lpBuf,nSize);//从源文件读取nSize字节
           }catch(CFileException* e){
              char* lpMsg;
              if(FormatMessage(FORMAT_MESSAGE_ALLOCATION_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e->m_1OsError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsg,0,NULL)>0)
              {
                  AfxMessageBox(lpMsg);
                  LocalFree(lpMsg);
              }
              e->Delete();
              return false;
           }
           try{//输出文件
               Out.Write(lpBuf,nSize);
           }catch(CFileException* e){
              char* lpMsg;
              if(FormatMessage(FORMAT_MESSAGE_ALLOCATION_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e->m_lOsError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPSTR)&lpMsg,0,NULL)>0)
              {
                  AfxMessageBox(lpMsg);
                  LocalFree(lpMsg);
              }
              e->Delete();
              return false;
           }
           nFileSize-=nSize;      
       }
       Out.Close();
       CFile::SetStatus(lpDestFile,rStatus);//同性设置
       delete[] lpBuf;
       return true;  
    }
      

  4.   

    1 BOOL CopyFile(
      LPCTSTR lpExistingFileName, // name of an existing file
      LPCTSTR lpNewFileName,      // name of new file
      BOOL bFailIfExists          // operation if file exists
    );这种方法应该是最简单的了。2 bool CopyFile(char * szExitFILE, char * szNewFiel)
    {
    std::ifstream in_file(szExitFiel);
    std::ofstream out_file(szNewFile);if(in_file)
    {
      return false;
    }
    if(out_file)
    {
      return false;
    }char ch;
    while(in_file.get(ch))
    {
      out_file.put(ch);
    }
    return true;
    }
      

  5.   

    若单纯的文件COPY ,则用CopyFile就行了,若是文件夹,且下有子文件夹则要用
    设置SHFILEOPSTRUC就行。如下:
                      SHFILEOPSTRUCT FileOp;
    FileOp.hwnd=m_hWnd;
    FileOp.wFunc=FO_COPY;


    //执行文件拷贝
    FileOp.pFrom=strScr;
    FileOp.pTo=strDst;
    FileOp.fFlags=FOF_ALLOWUNDO     ;
    FileOp.hNameMappings=NULL;
    FileOp.lpszProgressTitle=strTitle;

                     nOk=SHFileOperation(&FileOp);
                     if(nOk)
                TRACE("There is an error: %d\n",nOk);
                     else
    TRACE("SHFileOperation   finished successfully\n");