从路径中取目录/文件/创建目录/判断目录是否存在

本文介绍了如何使用Boost库中的文件系统组件来处理文件路径,包括获取目录路径、文件名、判断目录是否存在以及创建目录的方法。
用的是boost库中的文件系统,应该包含头文件#include <boost/filesystem.hpp>

一 .取路径中的目录
假设路径为/tmp/monkey/sems/123.txt ,要想得到/tmp/monkey/sems,方法如下:
boost::filesystem::path file_path(/tmp/monkey/sems/123.txt );
string file_dir = file_path.parent_path().native();//native()作用是转换为string类型,因为parent()返回类型是path
std::cout << file_dir << std::endl; //结果为/tmp/monkey/sems
注意,如果直接输出file_path.parent_path()结果也是/tmp/monkey/sems,就是需要赋值给string类型时应用native()进行转换.

二 .取路径中的文件名
若想得到123.txt,有
string file = file_path.file_name().native(); //file值就为123.txt

三 .判断目录是否存在
boost::filesystem::exists(file_dir);//若存在返回true

四 .创建目录
boost::filesystem::create_directories(file_dir); //可点击查看/tmp/monkey/sems存在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值