基础处理

#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. 使用jasypt加密配置的时候,报错:DecryptionException: Unable to decrypt

    前几天分享了一篇<Spring Boot 2.x基础教程:加密配置中的敏感信息> ,然后看到群里有小伙伴反应跟着这篇文章出现了这个异常com.ulisesbocchio.jasyptspr ...

  2. 数字孪生 3D 风电场,智慧风电之陆上风电

    前言 "十四五"期间,在传统产业数字化升级和绿色改造领域.绿色低碳城镇化和现代城市建设领域.绿色低碳消费领域,和可再生能源或电力系统建设等领域,总投资可以达到近 45 万亿,平均每 ...

  3. 基于java+springboot的求职招聘网站-求职招聘管理系统

    该系统是基于java+springboot开发的求职招聘网站.网上招聘管理系统.网上人才招聘系统.毕业生求职招聘系统.大学生求职招聘系统.校园招聘系统.企业招聘系统.是给师弟开发的毕业设计.大家学习过 ...

  4. 05-Shell索引数组变量

    1.介绍 Shell 支持数组(Array),数组是若干数据的集合,其中的每一份数据都称为数组的元素. 注意Bash Shell 只支持一维数组,不支持多维数组. 2.数组的定义 2.1 语法 在 S ...

  5. STM32 芯片锁死解决方法

    芯片锁死原因: 1.烧进去的工程对应器件与目标器件不一致: 2.烧进去的工程HSE_VALUE与目标板上晶振频率不一致: 3.... 解决方法: 1.工程设置 2.按住复位按键,或短接复位脚电容,点击 ...

  6. Laravel - 创建项目

    1,创建目录 ( 路径不要带有中文 ) 2,进入目录,执行下列命令 composer create-project --prefer-dist laravel/laravel project

  7. [转帖]TiUP 命令概览

    https://docs.pingcap.com/zh/tidb/stable/tiup-reference TiUP 在 TiDB 生态中承担包管理器的功能,管理着 TiDB 生态下众多的组件,如 ...

  8. Oracle AWR学习之二-利用ChatGPT编写一键获取AWR报告的脚本

    Oracle AWR学习之二-ChatGPT提升效率之n 背景 之前生成awr报告比较麻烦, 想着能够一键生成. 再辅以部分shell或者是python处理就可以进行细致的分析. 这一块其实还是比较简 ...

  9. Edge浏览器安装 wetab ChatGPT插件的简单步骤

    Edge浏览器安装 wetab ChatGPT插件的简单步骤 背景 首先感谢 神通的 李诺帆老师, 之前一直使用. https://chat.jubianxingqiu.com/#/chat/1002 ...

  10. [转帖]JAVA⽣态的微服务⽆侵⼊链路追踪

    https://v5.6-docs.rainbond.com/docs/v5.3/advanced-scenarios/devops/pinpoint/#pinpoint%E7%AE%80%E4%BB ...