兔子的技术博客

兔子

   :: 首页 :: 联系 :: 聚合  :: 管理
  202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

留言簿(10)

最新评论

阅读排行榜

评论排行榜

操作系统提供的API通常不支持直接拷贝目录树。不过,可以通过递归的方法实现。下面,我们用boost的filesystem库实现该功能。

 

 

  1. // recusively copy file or directory from $src to $dst
  2. void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst)
  3. {
  4. if (! boost::filesystem::exists(dst))
  5. {
  6. boost::filesystem::create_directories(dst);
  7. }
  8. for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it)
  9. {
  10. const boost::filesystem::path newSrc = src / it->filename();
  11. const boost::filesystem::path newDst = dst / it->filename();
  12. if (boost::filesystem::is_directory(newSrc))
  13. {
  14. CopyFiles(newSrc, newDst);
  15. }
  16. else if (boost::filesystem::is_regular_file(newSrc))
  17. {
  18. boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
  19. }
  20. else
  21. {
  22. _ftprintf(stderr, "Error: unrecognized file - %s", newSrc.string().c_str());
  23. }
  24. }
  25. }
是不是很简洁呢?该函数不仅可以拷贝目录树,还可以拷贝单个文件,而且输入参数可以是相对路径,您可以试试。
转自:http://blog.csdn.net/ganxinjiang/article/details/6088323
posted on 2011-11-01 11:01 会飞的兔子 阅读(4057) 评论(0)  编辑 收藏 引用 所属分类: C++库,组件

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理