How can I copy a directory using Boost Filesystem

后端 未结 4 1731
栀梦
栀梦 2020-12-08 14:40

How can I copy a directory using Boost Filesystem? I have tried boost::filesystem::copy_directory() but that only creates the target directory and does not copy the contents

4条回答
  •  感情败类
    2020-12-08 15:06

    Since C++17 you don't need boost for this operation anymore as filesystem has been added to the standard.

    Use std::filesystem::copy

    #include 
    #include 
    namespace fs = std::filesystem;
    
    int main()
    {
        fs::path source = "path/to/source/folder";
        fs::path target = "path/to/target/folder";
    
        try {
            fs::copy(source, target, fs::copy_options::recursive);
        }
        catch (std::exception& e) { // Not using fs::filesystem_error since std::bad_alloc can throw too.
            // Handle exception or use error code overload of fs::copy.
        }
    }
    

    See also std::filesystem::copy_options.

提交回复
热议问题