您的位置:首页 > 其它

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

2014-06-21 14:12 316 查看
用的是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存在
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐