C++ Boost 文件系统相关函数
基础处理
#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 文件系统相关函数的更多相关文章
- boost 文件系统
第 9 章 文件系统 目录 9.1 概述 9.2 路径 9.3 文件与目录 9.4 文件流 9.5 练习 该书采用 Creative Commons License 授权 9.1. 概述 库 Boo ...
- PHP中的文件系统函数(一)
从这篇文章开始,我们将学习一系列的 PHP 文件系统相关函数.其实这些函数中,有很多都是我们经常用到的,大家并不需要刻意地去记住它们,只要知道有这么个东西,在使用的时候记得来查文档就可以了. 文件路径 ...
- 环境搭建文档——Windows下的Git搭建详解
Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...
- PHP 如何 安全配置
配置选项 phpinfo( ) 函数可用于php.ini文件的定位 A.1. allow_url_fopen 选项允许你如同本地文件一样引用远程资源: 我推荐关闭allow_url_fopen选项,除 ...
- #Git 详细中文安装教程
Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...
- Git的使用--如何安装和使用 github,让小白不在那么白 (一)(超详解)
简介 刚开始写了关于如何将本地代码上传到github上,但是有些小伙伴们不清楚如何安装Git,这一篇就给小伙伴们普及一下Git的安装和使用.适合刚开始用git的小白,大神或者大佬请绕道. 实际项目开发 ...
- Windows10下安装Git
Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理.具体安装步骤如下: 第一步:先从官网下载最新版本的Git 官网地址:https://git-scm.com/do ...
- 在 windows 上安装 git 2.22
下载 by win 下载地址:https://git-scm.com/download/win 如下图.选择对应的版本下载: 安装 by win 1.双击下载好的git安装包.弹出提示框.如下图: 2 ...
- 在 windows 上安装 git 2.15
下载 by win 下载地址:https://git-scm.com/download/win 如下图.选择对应的版本下载: 安装 by win 1.双击下载好的git安装包.弹出提示框.如下图: 2 ...
- 《PHP扩展学习系列》系列分享专栏
<PHP扩展学习系列>系列分享专栏 <PHP扩展学习系列>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/20177 ...
随机推荐
- 国内加速访问Github的办法
说明 自从GitHub私有库免费后,又涌入了一大批开发爱好者. 但国内访问GitHub的速度实在是慢得一匹,在clone仓库时甚至只有10k以下的速度,大大影响了程序员的交友效率. 国内加速访问Git ...
- 【JAVA基础】日志管理
LOGGER.debug("Request uri: {}, headers: {}", signedRequest.getURI(), signedRequest.getAllH ...
- Codeforce:723A. The New Year: Meeting Friends (水题)
题意:有三个好朋友的家都住在x轴的不同坐标,问新年的时候三个朋友之间问候走的最短距离 max{(a,b,c)} - min{(a,b,c)} 即可 编译器由 VS2017 切换到VScode使用,纪念 ...
- 【有奖体验】AI 都这么厉害了,可以看图生成文字描述!
立即体验基于函数计算部署[图生文]一键部署图像描述模型: https://developer.aliyun.com/topic/aigc_fc 人工智能生成内容(Artificial Intellig ...
- Java虚拟机——类加载器深入剖析
一.java虚拟机与程序的生命周期 在如下几种情况下,java虚拟机将结束生命周期: 1.执行了System.exit()方法 2.程序正常执行结束 3.程序在执行过程中遇到了异常或错误而异常终止 4 ...
- NodeJS开发服务端实现文件上传下载和数据增删改查
本文主要讲解已NodeJS作为服务器完成文件的上传下载和数据增删改查,前端框架为Vue3,UI框架为element-plus,Node版本为V16.14.2. 项目场景模拟是开发一个项目管理的系统,支 ...
- sipp3.6带媒体测试方案
概述 SIP压测工具sipp,免费,开源,功能足够强大,配置灵活,优点多. 本文档介绍sipp工具如何带媒体测试,并介绍如何制作可用的媒体文件(G729和PCMA). 环境 centos7.9 fre ...
- linux 通过docker安装 elasticsearch-head
本文为博主原创,未经允许不得转载: 1. 使用docker安装 elasticsearch-head #拉取镜像 docker pull mobz/elasticsearch-head:5 #创建容器 ...
- 如何在 Photoshop 中制作 GIF 动画
您可能已经使用过一些在线 gif 生成器来快速生成 gif 图像,但这些在线生成器无法提供与 Photoshop 相同的灵活性和结果. gif 就像您可以在 Photoshop 中创建的迷你动画.当你 ...
- 【lvgl-micropython】官方源码之ports/unix 编译报错
lv_micropython/ports/unix make 报错 编译环境如下 这是缺少SDL2库导致的 sudo apt-get install libsdl2-2.0 sudo apt-get ...