【问题标题】:Copy directory content复制目录内容
【发布时间】:2016-05-19 14:12:15
【问题描述】:

我想将目录 (tmp1) 的内容复制到另一个目录 (tmp2)。 tmp1 可能包含文件和其他目录。我想使用 C/C++ 复制 tmp1 的内容(包括模式)。如果 tmp1 包含目录树,我想递归地复制它们。

什么是最简单的解决方案?

我找到了打开目录并读取每个条目并使用cp 命令复制它的解决方案。有更简单的解决方案吗?

【问题讨论】:

  • 您可能想检查一下:stackoverflow.com/questions/2180079/…
  • @Mirakurun 比我建议的要复杂得多,并且不复制文件的模式(并且不适用于目录)。
  • cp -a $src $dst - 否则:你已经做了什么?显示您的代码。这不是咨询网站。 C和C++也是不同的语言。选择一个
  • 我认为最简单的方法是使用现有的工具。只需使用cp -R

标签: c++ c linux


【解决方案1】:

我建议使用std::filesystem(从 C++17 开始合并到 ISO C++!)

无耻抄袭http://en.cppreference.com/w/cpp/filesystem/copy:

std::filesystem::copy("/dir1", "/dir3", std::filesystem::copy_options::recursive);

了解更多信息:

https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/api/a01832.html

experimental::filesystem linker error

【讨论】:

  • @Olaf:不要因为人们想提供帮助而惩罚他们。使用投票系统来表明答案是好还是不好(即它是否回答了问题)。您可以惩罚 OP 不努力,但这不是 cshu 的错。
【解决方案2】:

最近我也有同样的需求,所以我开发了下一段代码来解决这个问题。我希望它对处于相同情况的其他人有所帮助。

#include <iostream>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <windows.h>

using namespace std;

bool is_dir(const char* path);
void copyFile_(string inDir, string outDir);
void copyDir_(const char *inputDir, string outDir);

int main()
{

    string srcDir = "C:\\testDirectory";
    string destDir = "C:\\destDir";

    copyDir_(srcDir.c_str(),  destDir);

    return 0;



}
void copyDir_(const char *inputDir, string outDir)
{

    DIR *pDIR;
    struct dirent *entry;
    string tmpStr, tmpStrPath, outStrPath, inputDir_str = inputDir;

    if (is_dir(inputDir) == false)
    {
        cout << "This is not a folder " << endl;
        return;
    }


    if( pDIR = opendir(inputDir_str.c_str()) )
    {

        while(entry = readdir(pDIR)) // get folders and files names
        {

            tmpStr = entry->d_name;
            if( strcmp(entry->d_name, ".")  != 0 && strcmp(entry->d_name, "..") != 0 )
            {
                tmpStrPath = inputDir_str;
                tmpStrPath.append( "\\" );
                tmpStrPath.append( tmpStr );

                cout << entry->d_name;
                if (is_dir(tmpStrPath.c_str()))
                {
                    cout << "--> It's a folder" << "\n";
                    // Create Folder on the destination path
                    outStrPath = outDir;
                    outStrPath.append( "\\" );
                    outStrPath.append( tmpStr );
                    mkdir(outStrPath.c_str());

                    copyDir_(tmpStrPath.c_str(), outStrPath);
                }
                else
                {
                    cout << "--> It's a file"   << "\n";
                    // copy file on the destination path
                    outStrPath = outDir;
                    outStrPath.append( "\\" );
                    outStrPath.append( tmpStr );
                    copyFile_(tmpStrPath.c_str(), outStrPath.c_str());
                }
            }
        }
        closedir(pDIR);
    }
}

bool is_dir(const char* path)
{
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

void copyFile_(string inDir, string outDir)
{
    CopyFile(inDir.c_str(), outDir.c_str(), 1);
    DWORD Error = GetLastError();
}

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 2012-07-24
    • 2011-09-07
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多