如何拷贝一文件(如文本文件)到别的目录?用哪个函数呢?先谢了

解决方案 »

  1.   

    char buf[256];
    char buf1[256];
    SHFILEOPSTRUCT fo; memset(buf, 0, sizeof(buf));
    memset(buf1, 0, sizeof(buf1));
    memset(&fo, 0, sizeof(fo));
    strcpy(buf, "f:\\source");
    strcpy(buf1, "f:\\target");
    fo.wFunc = FO_MOVE;//复制则是FO_COPY
    fo.pFrom = buf;
    fo.pTo = buf1;
    fo.Flags = FOF_SIMPLEPROGRESS;
    SHFileOperation(&fo);
      

  2.   

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

  3.   

    system("copy xx.txt a:\\xx.txt");
      

  4.   

    直接使用copyfile或者movefile就可以了,自msdn中查具体的用法。
      

  5.   

    9.2.5 如何执行复制、删除、移动等操作
    问:如何移动一个目录及其子目录?
    问:如何删除一个目录及其子目录到回收站或者彻底删除?
    问:如何更改文件和目录的名称
    答:使用SHFileOperation API就可以进行文件和目录操作。这个API只有一个类型是SHFILEOPSTRUCT的参数,用于输入和输出信息.使用时容易出现的问题是忘记在SHFILEOPSTRUCT的pFrom和pTo指定的文件列表字符串末尾加上一个额外的0终止符,或者在pTo中指定了通配符或者相对路径。如果存在文件名冲突,新的文件名可能不是你指定的名字,而是以“复件 ”+原文件名这样的形式。这时候可能需要从结构的输出参数中获得更名后的文件名。下面的代码演示了在Win9x和ANSI程序中如何做到这一点。
    CString OldPath;
    CString NewPath;
    SHFILEOPSTRUCT shFileOp;//after filling other members:
    shFileOp.fFlags = FOF_WANTMAPPINGHANDLE 
                    | FOF_RENAMEONCOLLISION;shFileOp.lpszProgressTitle = "Test";
    shFileOp.fAnyOperationsAborted = FALSE;SHFileOperation(&shFileOp);if(!shFileOp.hNameMappings) 
     return; //there is nothing further to do, no file collisionstruct TMPMAP
    {
    int Indx;
    SHNAMEMAPPING *pMapping;
    };//this will give you a  pointer to the beginning of the 
    //array of SHNAMEMAPPING structures
    TMPMAP *pTmpMap = (TMPMAP*)shFileOp.hNameMappings;for(int in = 0; in < pTmpMap->Indx; in++)
    {
     SHNAMEMAPPING *pMap = &pTmpMap->pMapping[in]; //do the same thing for csOldPath
     /*********************************************************/
     char *buf = csNewPath.GetBufferSetLength(pMap->cchNewPath);strcpy(buf, (char*)pMap->pszOldPath); //see the result of
    this,for(int dw = 0 ; dw < 2 * pMap->cchNewPath - 1 ; dw+=2)
    {
    *buf = (pMap->pszNewPath[dw]);
    buf++;
    }
    buf = 0;
    csNewPath.ReleaseBuffer();  //and see the result of this.
    /**********************************************************/
    }//always free it if requested
    SHFreeNameMappings(shFileOp.hNameMappings);
    无论编译时你的程序指定的是UNICODE还是ANSI ,WINNT4.0版本以上的操作系统中返回的总是Unicode版本的SHNAMEMAPPING结构,。如果你需要你的代码在所有Windows版本中都适用,那么需要判断操作系统的版本,处理获得的SHNAMEMAPPINGA或者SHNAMEMAPPINGW。x = SHFileOperation(&shFileOp);
    ...
    if (fWin9x) {
    HandleAnsiNameMappings(shFileOp.hNameMappings);
    }
    else {
    HandleUnicodeNameMappings(shFileOp.hNameMappings);
    }
      

  6.   

    不用这么复杂吧!给个例子:
    MoveFileEx(sSourPath, sDestPath, MOVEFILE_COPY_ALLOWED);