void testFileSystem()
{
boost::filesystem::path path("/test/test1"); //初始化
boost::filesystem::path old_cpath = boost::filesystem::current_path(); //取得当前程序所在文件夹
boost::filesystem::path parent_path = old_cpath.parent_path();//取old_cpath的上一层父文件夹路径
boost::filesystem::path file_path = old_cpath / "file"; //path支持重载/运算符
std::string file_dir = file_path.string();
if (boost::filesystem::exists(file_path)) //推断文件存在性
{
std::string strPath = file_path.string();
int x = ;
}
else
{
//文件夹不存在;
boost::filesystem::create_directory(file_path); //文件夹不存在。创建
}
bool bIsDirectory = boost::filesystem::is_directory(file_path); //推断file_path是否为文件夹
boost::filesystem::recursive_directory_iterator beg_iter(file_path);
boost::filesystem::recursive_directory_iterator end_iter;
for (; beg_iter != end_iter; ++beg_iter)
{
if (boost::filesystem::is_directory(*beg_iter))
{
continue;
}
else
{
std::string strPath = beg_iter->path().string(); //遍历出来的文件名称
int x = ;
}
}
boost::filesystem::path new_file_path = file_path / "test.txt";
if (boost::filesystem::is_regular_file(new_file_path)) //推断是否为普通文件
{
UINT sizefile = boost::filesystem::file_size(new_file_path); //文件大小(字节)
int x = ;
}
boost::filesystem::remove(new_file_path);//删除文件new_file_path
} // 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->path();
const boost::filesystem::path newDst = dst / it->path();
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, _T("Error: unrecognized file - %s"), newSrc.string().c_str());
}
}
} bool CopyDirectory(const std::string &strSourceDir, const std::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替换,替换后的字符串保存在一个输出迭代器中
boost::algorithm::replace_first_copy(std::back_inserter(strRestorePath), strAppPath, strSourceDir, strDestDir);
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, boost::filesystem::copy_option::overwrite_if_exists, ec);
}
if (ec)
{
return false;
}
return true;
}

boost 文件操作的更多相关文章

  1. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  2. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  3. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  4. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  5. SQL Server附加数据库报错:无法打开物理文件,操作系统错误5

    问题描述:      附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  8. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

  9. Python的文件操作

    文件操作,顾名思义,就是对磁盘上已经存在的文件进行各种操作,文本文件就是读和写. 1. 文件的操作流程 (1)打开文件,得到文件句柄并赋值给一个变量 (2)通过句柄对文件进行操作 (3)关闭文件 现有 ...

随机推荐

  1. 用状态机表示SFC中的并行分支

    过去一直认为,状态机表示SFC会不会是任务复杂化,这次简单实验了一下,感觉还可以.请看下面的控制. 在SFC中,A和B是一对并行分支,汇合后转移到C分支中,怎么了用状态机表示呢?这里我们在状态机里分别 ...

  2. ctf题目writeup(6)

    2019.2.2 依旧是bugku上面的题目,地址:https://ctf.bugku.com/challenges 1. 解压后是60多个out.zip,都是真加密,里面都是1kb的data.txt ...

  3. gvim编码配置知识

    配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题     Vim / gVim 在中文 Windows 下的字符编码有两个问题: 默认没有编码检测功能 如果一个 ...

  4. ABS(引数と同じ大きさの正の数を返す)

    ABS 関数 [数値] 数値式の絶対値を返します. 構文 ABS( numeric-expression ) パラメータ numeric-expression   絶対値が返される数値. 戻り値 数値 ...

  5. 开启TCP BBR拥塞控制算法

    原文来自:https://github.com/iMeiji/shadowsocks_install/wiki/%E5%BC%80%E5%90%AFTCP-BBR%E6%8B%A5%E5%A1%9E% ...

  6. 触发显示和隐藏 div

    <script> window.onload = function(){ var oDiv1 = document.getElementById("div1"); va ...

  7. 在Linux中安装和配置OpenVPN Server的最简便方法!

    本文介绍了如何在基于RPM和DEB的系统中安装和配置OpenVPN服务器.我们在本文中将使用一个名为openvpn-install的脚本,它使整个OpenVPN服务器的安装和配置过程实现了自动化.该脚 ...

  8. Java byte 位移操作 注意事项

    转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...

  9. svn 用cmd命令行启动服务

    部署好svn 服务器后,用cmd命令行 svnserve -d -r [仓库地址] 启动服务,这样别的用户可以通过网络访问svn服务器了.

  10. APP功能性测试-3

    定义:兼容测试就是指软件在特定的硬件平台,不同的应用软件之间,不同的操作系统平台上,不同的网络等环境中是否能够正常的运行的测试  (会不会产生不兼容) 兼容性测试的作用 进一步提高产品质量 和其他软件 ...