【问题标题】:How to copy a file from a folder to another folder如何将文件从一个文件夹复制到另一个文件夹
【发布时间】:2012-02-03 07:27:21
【问题描述】:

如何使用 C++ 将文件从一个文件夹复制到另一个文件夹?

【问题讨论】:

  • 你想使用命令cp还是为cp写一个克隆?
  • 我想为cp写一个克隆
  • 我不想执行命令。成功复制后,我想从第一个文件夹中删除文件。需要帮忙。欣赏
  • "复制成功后我想从第一个文件夹中删除文件。"?您的意思是要移动文件?
  • @Johnsyweb 听起来像是另一个 X-Y 问题。而不是问如何去做他们真正想做的事;他们询问如何执行各个步骤的问题。至少这一步对人们来说是有用的一步

标签: c++ linux file-io


【解决方案1】:

这应该是所需的最少代码:

#include <fstream>

// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
    std::ifstream src(SRC, std::ios::binary);
    std::ofstream dest(DEST, std::ios::binary);
    dest << src.rdbuf();
    return src && dest;
}

int main(int argc, char *argv[])
{
    return copyFile(argv[1], argv[2]) ? 0 : 1;
}

它掩盖了一些潜在的复杂问题:错误处理、文件名字符编码......但可以让你开始。

【讨论】:

    【解决方案2】:

    使用来自 C++17 的 std::filesystem::copy_file

    #include <exception>
    #include <filesystem>
    #include <iostream>
    
    namespace fs = std::filesystem;
    
    int main()
    {
        fs::path sourceFile = "path/to/sourceFile.ext";
        fs::path targetParent = "path/to/target";
        auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
    
        try // If you want to avoid exception handling, then use the error code overload of the following functions.
        {
            fs::create_directories(targetParent); // Recursively create target directory if not existing.
            fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
        }
        catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.  
        {
            std::cout << e.what();
        }
    }
    

    我使用std::filesystem::path::filename 来检索源文件名,而无需手动输入。但是,使用std::filesystem::copy,您可以完全省略将文件名传递给目标路径:

    fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
    

    使用std::filesystem::copy_options 更改这两个函数的行为。

    【讨论】:

      【解决方案3】:

      如果您愿意使用 Boost C++ 库,请查看filesystem::copy_file()

      这里有一个关于 copy_file() 的问题:

      How to use copy_file in boost::filesystem?

      【讨论】:

      • 现在 copy_file 是 c++ 的一部分,从 c++14 开始。
      【解决方案4】:

      你可以这样做。

      1.包含c++库&lt;windows.h&gt;

      2.使用功能

      CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
      

      3.全部完成:)

      【讨论】:

        【解决方案5】:

        下面的代码会将所有文件从一个目录复制到另一个目录。

        它的 C++ 工作代码

        #include <windows.h>
        
        /*
        BOOL Copy(char r_szPath[1024], char r_szDir[1024])
        {
        char l_szTemp[2048] = {0};
        sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
        
        if(IsDirectory(
        }*/
        
        #include <stdio.h>
        #include<conio.h>
        
        BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
        {
        WIN32_FIND_DATA FindFileData;
        HANDLE hFind;
        char l_szTmp[1025] = {0};
        memcpy(l_szTmp,r_szSrcPath,1024);
        
        
        char l_szSrcPath[1025] = {0};
        char l_szDesPath[1025] = {0};
        memcpy(l_szSrcPath,r_szSrcPath,1024);
        memcpy(l_szDesPath,r_szDesPath,1024);
        
        char l_szNewSrcPath[1025] = {0};
        char l_szNewDesPath[1025] = {0};
        
        strcat(l_szTmp,"*");
        
        hFind = FindFirstFile(l_szTmp, &FindFileData);
        if(hFind == NULL) return FALSE;
        
        do
        {
        
        if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
        if(strcmp(FindFileData.cFileName,"."))
        {
        if(strcmp(FindFileData.cFileName,".."))
        {
        printf ("The Directory found is %s<BR>, FindFileData.cFileName);
        
        sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
        
        sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
        CreateDirectory(l_szNewDesPath,NULL);
        __Copy(l_szNewSrcPath,l_szNewDesPath);
        }
        }
        }
        else
        {
        printf ("The File found is %s<BR>, FindFileData.cFileName);
        char l_szSrcFile[1025] = {0};
        char l_szDesFile[1025] = {0};
        sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
        sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
        BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
        
        }
        
        
        }
        while(FindNextFile(hFind, &FindFileData));
        FindClose(hFind);
        return TRUE;
        }
        
        
        int main(int argc, char *argv[])
        {
        __Copy("C:\fcdb\","E:\sandy\");
        getch();
        return 0;
        }
        

        【讨论】:

        • 欢迎来到 SO... 为了改进您的帖子,请考虑删除它或解决这些问题 1. 这不是工作代码,您的 if 语句中有大量关于 '.' 的错误。和 '..'.. 并且不要忘记包含中省略空格的错字。 2. 你没有回答手头的问题。您回答了您创建的问题。 3. 问题是针对 C++ 和 Ubuntu... 使用 windows 特定的东西在这里没有用 4. 请缩进你的代码以便它可读
        最近更新 更多