<tfoot id='kqH9F'></tfoot>

  • <i id='kqH9F'><tr id='kqH9F'><dt id='kqH9F'><q id='kqH9F'><span id='kqH9F'><b id='kqH9F'><form id='kqH9F'><ins id='kqH9F'></ins><ul id='kqH9F'></ul><sub id='kqH9F'></sub></form><legend id='kqH9F'></legend><bdo id='kqH9F'><pre id='kqH9F'><center id='kqH9F'></center></pre></bdo></b><th id='kqH9F'></th></span></q></dt></tr></i><div id='kqH9F'><tfoot id='kqH9F'></tfoot><dl id='kqH9F'><fieldset id='kqH9F'></fieldset></dl></div>

        <bdo id='kqH9F'></bdo><ul id='kqH9F'></ul>
    1. <small id='kqH9F'></small><noframes id='kqH9F'>

      <legend id='kqH9F'><style id='kqH9F'><dir id='kqH9F'><q id='kqH9F'></q></dir></style></legend>

      1. 如何使用 Boost Filesystem 复制目录

        时间:2023-07-19
          <legend id='ILUwO'><style id='ILUwO'><dir id='ILUwO'><q id='ILUwO'></q></dir></style></legend>

        1. <small id='ILUwO'></small><noframes id='ILUwO'>

            <bdo id='ILUwO'></bdo><ul id='ILUwO'></ul>
              <tbody id='ILUwO'></tbody>
                <i id='ILUwO'><tr id='ILUwO'><dt id='ILUwO'><q id='ILUwO'><span id='ILUwO'><b id='ILUwO'><form id='ILUwO'><ins id='ILUwO'></ins><ul id='ILUwO'></ul><sub id='ILUwO'></sub></form><legend id='ILUwO'></legend><bdo id='ILUwO'><pre id='ILUwO'><center id='ILUwO'></center></pre></bdo></b><th id='ILUwO'></th></span></q></dt></tr></i><div id='ILUwO'><tfoot id='ILUwO'></tfoot><dl id='ILUwO'><fieldset id='ILUwO'></fieldset></dl></div>

                  <tfoot id='ILUwO'></tfoot>
                  本文介绍了如何使用 Boost Filesystem 复制目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何使用 Boost 文件系统复制目录?我试过 boost::filesystem::copy_directory() 但它只创建目标目录而不复制内容.

                  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.

                  推荐答案

                  bool copyDir(
                      boost::filesystem::path const & source,
                      boost::filesystem::path const & destination
                  )
                  {
                      namespace fs = boost::filesystem;
                      try
                      {
                          // Check whether the function call is valid
                          if(
                              !fs::exists(source) ||
                              !fs::is_directory(source)
                          )
                          {
                              std::cerr << "Source directory " << source.string()
                                  << " does not exist or is not a directory." << '
                  '
                              ;
                              return false;
                          }
                          if(fs::exists(destination))
                          {
                              std::cerr << "Destination directory " << destination.string()
                                  << " already exists." << '
                  '
                              ;
                              return false;
                          }
                          // Create the destination directory
                          if(!fs::create_directory(destination))
                          {
                              std::cerr << "Unable to create destination directory"
                                  << destination.string() << '
                  '
                              ;
                              return false;
                          }
                      }
                      catch(fs::filesystem_error const & e)
                      {
                          std::cerr << e.what() << '
                  ';
                          return false;
                      }
                      // Iterate through the source directory
                      for(
                          fs::directory_iterator file(source);
                          file != fs::directory_iterator(); ++file
                      )
                      {
                          try
                          {
                              fs::path current(file->path());
                              if(fs::is_directory(current))
                              {
                                  // Found directory: Recursion
                                  if(
                                      !copyDir(
                                          current,
                                          destination / current.filename()
                                      )
                                  )
                                  {
                                      return false;
                                  }
                              }
                              else
                              {
                                  // Found file: Copy
                                  fs::copy_file(
                                      current,
                                      destination / current.filename()
                                  );
                              }
                          }
                          catch(fs::filesystem_error const & e)
                          {
                              std:: cerr << e.what() << '
                  ';
                          }
                      }
                      return true;
                  }
                  

                  用法:

                  copyDir(boost::filesystem::path("/home/nijansen/test"), boost::filesystem::path("/home/nijansen/test_copy")); (Unix)

                  copyDir(boost::filesystem::path("C:\Users\nijansen\test"), boost::filesystem::path("C:\Users\nijansen\test2")); (Windows)

                  据我所知,最坏的情况是什么也没有发生,但我不会承诺任何事情!使用风险自负.

                  As far as I see, the worst that can happen is that nothing happens, but I won't promise anything! Use at your own risk.

                  请注意,您要复制到的目录不得存在.如果您尝试复制的目录中的目录无法读取(想想权限管理),它们将被跳过,但仍应复制其他目录.

                  Please note that the directory you're copying to must not exist. If directories within the directory you are trying to copy can't be read (think rights management), they will be skipped, but the other ones should still be copied.

                  更新

                  根据注释重构功能.此外,该函数现在返回成功结果.如果不满足给定目录或源目录中任何目录的要求,它将返回false,但如果无法复制单个文件,则返回false.

                  Refactored the function respective to the comments. Furthermore the function now returns a success result. It will return false if the requirements for the given directories or any directory within the source directory are not met, but not if a single file could not be copied.

                  这篇关于如何使用 Boost Filesystem 复制目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为现有变量创建一个 boost::shared_ptr 下一篇:在 Windows 中构建 boost 的一个子集

                  相关文章

                  <i id='py7XC'><tr id='py7XC'><dt id='py7XC'><q id='py7XC'><span id='py7XC'><b id='py7XC'><form id='py7XC'><ins id='py7XC'></ins><ul id='py7XC'></ul><sub id='py7XC'></sub></form><legend id='py7XC'></legend><bdo id='py7XC'><pre id='py7XC'><center id='py7XC'></center></pre></bdo></b><th id='py7XC'></th></span></q></dt></tr></i><div id='py7XC'><tfoot id='py7XC'></tfoot><dl id='py7XC'><fieldset id='py7XC'></fieldset></dl></div>
                1. <legend id='py7XC'><style id='py7XC'><dir id='py7XC'><q id='py7XC'></q></dir></style></legend>

                    <small id='py7XC'></small><noframes id='py7XC'>

                    • <bdo id='py7XC'></bdo><ul id='py7XC'></ul>
                  1. <tfoot id='py7XC'></tfoot>