您的位置:首页 > 其它

应用boost::filesystem递归拷贝目录树

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

// recusively copy file or directory from $src to $dst
void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst)
{
if (! boost::filesystem::exists(dst))
{
boost::filesystem::create_directories(dst);
}
for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it)
{
const boost::filesystem::path newSrc = src / it->filename();
const boost::filesystem::path newDst = dst / it->filename();
if (boost::filesystem::is_directory(newSrc))
{
CopyFiles(newSrc, newDst);
}
else if (boost::filesystem::is_regular_file(newSrc))
{
boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
}
else
{
_ftprintf(stderr, "Error: unrecognized file - %s", newSrc.string().c_str());
}
}
}


是不是很简洁呢?该函数不仅可以拷贝目录树,还可以拷贝单个文件,而且输入参数可以是相对路径,您可以试试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐