C++文件与文件夹操作(3)--文件与文件夹复制
来源:互联网 发布:软件安装管家 编辑:程序博客网 时间:2024/05/23 01:52
(1)文件复制
#include "stdafx.h"
#include <iostream>
#include <string>
#include "boost/filesystem.hpp"
using namespace std;
namespace bfs = boost::filesystem;
int main(int argc, char* argv[])
{
string rawFile= "D:/file/raw.txt";
string copyFile="D:/file/copy.txt";
bfs::copy_file(rawFile,copyFile,bfs::copy_option::overwrite_if_exists);
return 0;
}
注:其中bfs::copy_option为复制选项,若文件存在进行复制。
(2)文件夹复制
#include "stdafx.h"
#include <iostream>
#include <string>
#include "boost/filesystem.hpp"
#include "boost/algorithm/string.hpp"
using namespace std;
namespace bfs = boost::filesystem;
void copyDirector(const string& strSourceDir, const string& strDestDir); // 函数声明
int main(int argc, char* argv[])
{
string rawDir = "D:/raw/";
string copyDir = "D:/copy/";
copyDirector(rawDir, copyDir);
return 0;
}
void copyDirector(const string& strSourceDir, const string& strDestDir)
{
boost::filesystem::recursive_directory_iterator end; //设置遍历结束标志,用recursive_directory_iterator即可循环的遍历目录
boost::system::error_code ec;
for (boost::filesystem::recursive_directory_iterator pos(strSourceDir); pos != end; ++pos)
{
//过滤掉目录和子目录为空的情况
if(boost::filesystem::is_directory(*pos))
continue;
std::string strAppPath = boost::filesystem::path(*pos).string();
std::string strRestorePath;
//replace_first_copy在algorithm/string头文件中,在strAppPath中查找strSourceDir字符串,找到则用strDestDir替换,替换后的字符串保存在一个输出迭代器中
size_t strSourceDirLength = strSourceDir.length();
string strSourceDirNew = strSourceDir;
if (strSourceDir[strSourceDirLength-1]=='/')
{
strSourceDirNew = strSourceDirNew.erase(strSourceDirLength-1,1);
}
size_t strDestDirLength = strDestDir.length();
string strDestDirNew = strDestDir;
if (strDestDir[strDestDirLength-1]=='/')
{
strDestDirNew = strDestDirNew.erase(strDestDirLength-1,1);
}
boost::replace_first_copy(std::back_inserter(strRestorePath), strAppPath, strSourceDirNew, strDestDirNew);
if(!boost::filesystem::exists(boost::filesystem::path(strRestorePath).parent_path()))
{
boost::filesystem::create_directories(boost::filesystem::path(strRestorePath).parent_path(), ec);
}
boost::filesystem::copy_file(strAppPath, strRestorePath, bfs::copy_option::overwrite_if_exists, ec);
}
/*
if(ec)
{
return false;
}
return true;
*/
}
- C++文件与文件夹操作(3)--文件与文件夹复制
- cp 复制文件与文件夹
- CentOS 复制文件与文件夹
- java复制文件与文件夹
- 批处理(复制文件夹与文件)命令
- 批处理(复制文件夹与文件)命令
- Io流,文件复制与文件夹复制
- C#文件与文件夹操作
- OC 文件与文件夹操作
- python文件与文件夹操作
- Qt 文件夹与文件操作
- 文件/文件夹创建、复制与删除
- [JAVA]文件操作--复制文件夹
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- Python文件夹与文件的操作
- 基于lnmp 一键安装 添加 Nginx 的模块(module)
- Java 类与对象
- 【12】FtpWebRequest上传下载
- eclipse的控制台显示有问题,关闭Limit console output
- 保留道路
- C++文件与文件夹操作(3)--文件与文件夹复制
- 只有三星手机内存溢出oom
- 黑客六道:你需要是自己写代码——致正在学习的朋友们
- HDU 1370 Biorhythms(中国剩余定理 + 拓展欧几里得)
- 20170821
- 界面懒加载优化问题
- 正则表达式集
- Java-多个多线程求和例子以及各种方式的执行效率对比
- 为什么用mui框架写的头部,放进安卓原生态开发里边,头部不见了,只有头部以下的内容?