基础处理

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp> using namespace std;
using namespace boost;
using namespace boost::filesystem; int main(int argc, char *argv[])
{
// 判断路径是否为空
filesystem::path path_a;
if (path_a.empty())
cout << "文件路径为空" << endl; // 提取区间字符
char str[] = "hello / lyshark";
filesystem::path path_b(str + 6, str + 7);
cout << "区间提取: " << path_b << endl; // 追加字符串序列
path_b /= "etc";
string filename_a = "xinetd.conf";
path_b.append(filename_a.begin(), filename_a.end());
cout << "追加后的字符串: " << path_b << endl; // 追加字符串concat
filesystem::path path_c(str + 6, str + 7);
string filename_b = "lyshark.log";
path_c += "etc";
path_c.concat(filename_b.begin(), filename_b.end());
cout << "追加后的字符串: " << path_c << endl; // 路径切割函数
filesystem::path path_d("/usr/loca/include/lyshark.hpp");
cout << "原始路径: " << path_d.string() << endl;
cout << "返回主路径: " << path_d.parent_path() << endl;
cout << "返回主文件名: " << path_d.stem() << endl;
cout << "返回全文件名: " << path_d.filename() << endl;
cout << "返回扩展名: " << path_d.extension() << endl; // 返回绝对相对路径
filesystem::path path_e("c://windows/lyshark.hpp");
cout << "是否为绝对路径: " << path_e.is_absolute() << endl;
cout << "返回根分区盘符: " << path_e.root_name() << endl;
cout << "返回根路径: " << path_e.root_directory() << endl;
cout << "返回分区绝对盘符: " << path_e.root_path() << endl;
cout << "返回相对路径: " << path_e.relative_path() << endl; cout << "只保留路径: " << path_e.replace_extension() << endl;
cout << "修改后缀扩展: " << path_e.replace_extension("jsp") << endl; // 字符路径分割
filesystem::path path_f("c://windows/system32/etc/lyshark.cpp");
BOOST_FOREACH(auto& x, path_f)
{
cout << x << endl;
} getchar();
return 0;
}

文件属性操作:

#include <iostream>
#include <fstream> #include <boost/optional.hpp>
#include <boost/filesystem.hpp> using namespace std;
using namespace boost;
using namespace boost::filesystem; int main(int argc, char *argv[])
{
namespace fs = boost::filesystem; // 获取文件大小
fs::path ptr("c://lyshark.exe");
try
{
cout << "文件大小: " << fs::file_size(ptr) / 1024 << " KB" << endl;
}
catch (filesystem_error& e)
{
cout << "异常: " << e.path1() << e.what() << endl;
} // 获取修改时间
std::time_t timer = filesystem::last_write_time(ptr);
cout << "(修改时间)时间戳: " << timer << endl; // 获取盘符容量
space_info size = filesystem::space("c://");
cout << "总容量: " << size.capacity / 1024 / 1024 << " MB" << endl;
cout << "剩余容量: " << size.free / 1024 / 1024 << " MB" << endl;
cout << "可用容量: " << (size.capacity - size.free) / 1024 / 1024 << " MB" << endl; // 文件与目录判断
filesystem::path root = "c://lyshark.exe";
cout << "是否为目录: " << is_directory(root) << endl;
cout << "是否存在: " << exists(root) << endl; // 文件状态与权限检测
filesystem::path root_type = "c://lyshark.exe";
cout << "类型检测: " << filesystem::status(root_type).type() << endl;
cout << "权限检测: " << filesystem::status(root_type).permissions() << endl; getchar();
return 0;
}

文件操作:

#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp> using namespace std;
using namespace boost; int main(int argc, char *argv[])
{
namespace fs = boost::filesystem; // 路径拼接
fs::path current_path = fs::current_path();
cout << "当前目录: " << current_path << endl; fs::path init_path = fs::initial_path();
cout << "初始目录: " << init_path << endl; fs::path file_path = current_path / "lyshark";
cout << "拼接后的路径: " << file_path << endl; // 判断文件是否存在
filesystem::path p_tree = "c://a/abc.txt";
cout << "文件是否存在: " << filesystem::exists(p_tree) << endl;
cout << "文件是否为空: " << filesystem::is_empty(p_tree) << endl; // 创建空目录
auto directory_ref = filesystem::create_directory(filesystem::path("c://11111"));
cout << "是否创建成功: " << directory_ref << endl; // 创建递归目录
filesystem::path root_tree = filesystem::path("c://");
auto directorys_ref = filesystem::create_directories(root_tree / "sub_dir_a" / "sub_dir_b");
cout << "是否创建成功: " << directorys_ref << endl; // 文件或目录拷贝命令
filesystem::copy_directory("c://lyshark", "d://lyshark");
filesystem::copy_file("c://lyshark.exe", "d://lyshark/lyshark.exe"); // 重命名文件或目录
filesystem::rename("c://lyshark.exe", "c://www.exe"); // 删除文件或目录
auto remove_ref = filesystem::remove("c://lyshark");
cout << "是否已删除(空目录): " << remove_ref << endl; auto remove_all_ref = filesystem::remove_all("c://lyshark");
cout << "是否已删除(非空目录): " << remove_all_ref << endl; getchar();
return 0;
}

迭代目录: 针对特定目录的枚举操作,或过滤查找特定文件。

#include <iostream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/xpressive/xpressive.hpp> using namespace std;
using namespace boost;
using namespace boost::filesystem;
using namespace boost::xpressive; // directory_iterator 不支持深层遍历,只能遍历一层
void disk_foreach()
{
// 枚举当前路径下的所有目录
directory_iterator end;
for (directory_iterator pos("c://"); pos != end; ++pos)
{
cout << *pos << endl;
} // 定义迭代器实现枚举目录
typedef std::pair<directory_iterator, directory_iterator> dir_range;
dir_range dir(directory_iterator("c://"), directory_iterator()); // 定义迭代起始与结束令 BOOST_FOREACH(auto &x, dir)
{
cout << x << endl;
}
} // 遍历文件函数版
void GetFilePath(const string& pathName, std::vector <std::string> &recusiveFileVec)
{
boost::filesystem::recursive_directory_iterator rdi(pathName);
boost::filesystem::recursive_directory_iterator end_rdi;
recusiveFileVec.empty(); for (; rdi != end_rdi; rdi++)
{
if (is_directory(*rdi))
{
//std::cout << *rdi << "is pathName" << std::endl;
}
else
{
recusiveFileVec.push_back(rdi->path().string());
//std::cout << *rdi << " is a file" << std::endl;
}
}
} // 递归迭代目录(低效率版)
void recursive_dir(const path& dir)
{
directory_iterator end; for (directory_iterator pos(dir); pos != end; ++pos)
{
// 判断是否为目录
if (is_directory(*pos))
{
recursive_dir(*pos);
}
else
{
cout << *pos << endl;
}
}
} // 递归目录(高效版)
void recursive_dir_new(const path& dir)
{
// level() 返回当前目录深度
recursive_directory_iterator end;
for (recursive_directory_iterator pos(dir); pos != end; ++pos)
{
// 只输出只有一层的路径
if (pos.level() == 0)
{
cout << "目录深度: " << pos.level() << " 路径: " << *pos << endl;
}
}
} // 递归寻找文件(不支持正则处理)
boost::optional<path> recursive_find_file(const path& dir, const string& filename)
{
// 定义返回值类型,这个optional返回容器
typedef boost::optional<path> result_type; // 检测如果不是目录则直接退出
if (!exists(dir) || !is_directory(dir))
{
return result_type();
} recursive_directory_iterator end;
for (recursive_directory_iterator pos(dir); pos != end; ++pos)
{
// 如果不是目录并且文件名相同则返回这个路径
if (!is_directory(*pos) && pos->path().filename() == filename)
{
return result_type(pos->path());
}
}
return result_type();
} // 递归寻找文件(支持正则处理)
std::vector<path> recursive_find_file_regx(const path& dir, const string& filename)
{
// 定义正则表达式静态对象
static boost::xpressive::sregex_compiler rc; // 先判断正则对象是否正常
if (!rc[filename].regex_id())
{
// 处理文件名 将.替换为\\. 将 * 替换为 .*
std::string str = replace_all_copy(replace_all_copy(filename, ".", "\\."), "*", ".*");
rc[filename] = rc.compile(str); // 创建正则
} typedef std::vector<path> result_type;
result_type vct;
if (!exists(dir) || !is_directory(dir))
{
return vct;
} recursive_directory_iterator end;
for (recursive_directory_iterator pos(dir); pos != end; ++pos)
{
if (!is_directory(*pos) && regex_match(pos->path().filename().string(), rc[filename]))
{
// 如果找到了就加入到vector里面
vct.push_back(pos->path());
}
}
return vct;
} // 文件复制操作函数
size_t my_copy_file(const path& from_dir, const path& to_dir, const string& filename = "*")
{
// 判断源文件路径必须为目录
if (!is_directory(from_dir))
{
cout << "原始文件不能为文件" << endl;
return 0;
} // 查找原目录下的所有文件
auto vec = recursive_find_file_regx(from_dir, filename);
if (vec.empty())
{
cout << "目录中没有文件,自动跳过拷贝" << endl;
return 0;
} path path_ptr;
for (auto& ptr : vec)
{
// 拆分基本路径与目标路径
path_ptr = to_dir / ptr.string().substr(from_dir.string().length()); // 判断并创建子目录
if (!exists(path_ptr.parent_path()))
{
create_directories(path_ptr.parent_path());
}
cout << "源文件: " << path_ptr.string() << " 拷贝到: " << to_dir.string() << endl; // 开始拷贝文件
boost::filesystem::copy_file(ptr, path_ptr);
}
cout << "拷贝总文件数: " << vec.size() << endl;
return vec.size();
} int main(int argc, char *argv[])
{
// 不使用通配符寻找
auto ref = recursive_find_file("c:\\lyshark", "123.txt");
if (ref)
cout << "找到文件: " << *ref << endl; // 使用通配符寻找
auto regx_ref = recursive_find_file_regx("c:\\lyshark", "*.txt"); cout << "找到文件: " << regx_ref.size() << endl;
for (boost::filesystem::path &ptr : regx_ref)
{
cout << "找到文件路径: " << ptr << endl;
} // 输出枚举内容
std::vector <std::string> file_path; GetFilePath("C://backup", file_path);
for (int x = 0; x < file_path.size(); x++)
{
std::cout << file_path[x] << std::endl;
} // 实现文件拷贝
my_copy_file("c:\\lyshark", "c:\\c"); getchar();
return 0;
}

文件流操作:

#include <iostream>
#include <fstream> #include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp> using namespace std;
using namespace boost; void get_file_in_path()
{
namespace fs = boost::filesystem; filesystem::path p("c://a/abc.txt");
fs::ifstream ifs(p); if (ifs.is_open() == 1)
{
cout << ifs.rdbuf() << endl;
}
} int main(int argc, char *argv[])
{
namespace fs = boost::filesystem; // 路径拼接
fs::path current_path = fs::current_path();
cout << "当前目录: " << current_path << endl; fs::path file_path = current_path / "lyshark";
cout << "拼接后的路径: " << file_path << endl;
getchar();
return 0;
}

C++ Boost 文件系统相关函数的更多相关文章

  1. boost 文件系统

    第 9 章 文件系统 目录 9.1 概述 9.2 路径 9.3 文件与目录 9.4 文件流 9.5 练习  该书采用 Creative Commons License 授权 9.1. 概述 库 Boo ...

  2. PHP中的文件系统函数(一)

    从这篇文章开始,我们将学习一系列的 PHP 文件系统相关函数.其实这些函数中,有很多都是我们经常用到的,大家并不需要刻意地去记住它们,只要知道有这么个东西,在使用的时候记得来查文档就可以了. 文件路径 ...

  3. 环境搭建文档——Windows下的Git搭建详解

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...

  4. PHP 如何 安全配置

    配置选项 phpinfo( ) 函数可用于php.ini文件的定位 A.1. allow_url_fopen 选项允许你如同本地文件一样引用远程资源: 我推荐关闭allow_url_fopen选项,除 ...

  5. #Git 详细中文安装教程

    Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...

  6. Git的使用--如何安装和使用 github,让小白不在那么白 (一)(超详解)

    简介 刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用.适合刚开始用git的小白,大神或者大佬请绕道. 实际项目开发 ...

  7. Windows10下安装Git

    Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...

  8. 在 windows 上安装 git 2.22

    下载 by win 下载地址:https://git-scm.com/download/win 如下图.选择对应的版本下载: 安装 by win 1.双击下载好的git安装包.弹出提示框.如下图: 2 ...

  9. 在 windows 上安装 git 2.15

    下载 by win 下载地址:https://git-scm.com/download/win 如下图.选择对应的版本下载: 安装 by win 1.双击下载好的git安装包.弹出提示框.如下图: 2 ...

  10. 《PHP扩展学习系列》系列分享专栏

    <PHP扩展学习系列>系列分享专栏   <PHP扩展学习系列>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/20177 ...

随机推荐

  1. Java异步编程详解

    在现代应用程序开发中,异步编程变得越来越重要,特别是在处理I/O密集型任务时.Java提供了一套强大的异步编程工具,使得开发者能够更有效地处理并发任务.本篇博文将深入探讨Java中异步编程的方方面面, ...

  2. 为什么 Go 和 Rust 语言都舍弃了继承?

    为什么go和rust语言都舍弃了继承? 舍弃了 Class 舍弃或弱化子类型 类的继承是一段儿弯路 OO 发明了继承,然后发现真正有意义的是 interface 的组合(更准确的说,是 Product ...

  3. # 0x54 动态规划-树形DP

    A.没有上司的舞会 基础树形DP emmm,蒟蒻发现自己的DP太辣鸡了...所以来练练DP,这题的话实际上应该算是树DP的入门题吧,转移还是挺好想的. 每次在每个节点都会有个选择,就是选还是不选,如果 ...

  4. AtCoder Beginner Contest 176 (ABC水题,D题01BFS,E数组处理)

    补题链接:Here A - Takoyaki 很容易看出 \(\frac{N + X - 1}{X} \times T\) B - Multiple of 9 给定一个很大的整数,问其是否是 \(9\ ...

  5. 牛客 | 小G的约数引起的对于 整数分块 学习

    整除分块是个啥:要求\(∑_{i = 1}^n{n/i}\) 的值,这时候暴力需要O(n)的时间.由于这个区间是连续的,且'/'是向下取整,当i不能整除k时,n/i会等于最小的i(也就是区间最左边的值 ...

  6. nextTick用法

  7. LeetCode-Go:一个使用 Go 语言题解 LeetCode 的开源项目

    在中国的 IT 环境里,大多数场景下,学习算法的目的在于通过笔试算法题. 但算法书林林总总,有时候乱花渐欲迷人眼. 杜甫有诗云:读书破万卷,下笔如有神.不管选择哪本书,只要深入学习,分层次,逐层进阶, ...

  8. npm, yarn和pnpm清理缓存

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  9. 精通 VS 调试技巧,学习与工作效率翻倍!

    ​ 欢迎大家来到贝蒂大讲堂 ​ 养成好习惯,先赞后看哦~ ​ 所属专栏:C语言学习 ​ 贝蒂的主页:Betty's blog ​ 1. 什么是调试 当我们写代码时候常常会遇见输出结果不符合我们预期的情 ...

  10. SQLServer 性能报表的学习与使用

    SQLServer 性能报表的学习与使用 背景 前面连续学习了 SQLServer如何优化等事宜. 但是一开始总是么有找到对应的问题解决思路 周天时想到了 SQLSERVER的MDW工具 但是并没有找 ...