标准库已集成std::filesystem
介绍
filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能,可以跨平台操作目录、文件,写出通用的脚本程序。
1.path的构造函数可以接受C字符串和string,也可以是一个指定首末迭代器字符串序列区间。
2.filesystem提供了一系列的文件名(或目录)检查函数。
3.有丰富的函数用于获取文件名、目录名、判断文件属性等等。
4.filesystem库使用异常来处理文件操作时发生的错误。
5.filesystem库提供一个文件状态类file_status及一组相关函数,用于检查文件的各种属性,如是否存在、是否是目录、是否是符号链接等。
6.filesystem提供了少量的文件属性操作,如windows下的只读、归档等,Linux下的读写权限等。
7.文件操作,如创建目录、文件改名、文件删除、文件拷贝等等。
8.basic_directory_iterator提供了迭代一个目录下所有文件的功能。
path类的基本用法
//注意 /= 和 += 的区别, /= 表示追加下级目录, += 仅仅是字符串的串接
boost::filesystem::path dir("/home/alan");
dir /= "data"; //追加下级目录
dir /= "test.txt";
std::cout << dir << std::endl; //"/home/alan/data/test.txt"
std::cout << dir.string() << std::endl; //转换成std::string 类型:/home/alan/data/test.txt
std::cout << dir.root_name()<< std::endl; //盘符名:""
std::cout << dir.root_directory()<< std::endl; //根目录:"/"
std::cout << dir.root_path()<< std::endl; //根路径:"/"
std::cout << dir.relative_path()<< std::endl; //相对路径:"home/alan/data/test.txt"
std::cout << dir.parent_path()<< std::endl; //上级目录:"/home/alan/data"
std::cout << dir.filename()<< std::endl; //文件名:"test.txt"
std::cout << dir.stem()<< std::endl; //不带扩展的文件名:"test"
std::cout << dir.extension()<< std::endl; //扩展名:".txt"
std::cout << boost::filesystem::change_extension(dir, ".xml"); //更换扩展名
boost::filesystem常用方法:
#include <boost/filesystem.hpp>
boost::filesystem::path init_path("/home/alan/test"); //初始化
boost::filesystem::path curr_path = boost::filesystem::current_path(); //取得当前程序所在文件夹
boost::filesystem::path parent_path = curr_path.parent_path();//取得curr_path的上一层父文件夹路径
boost::filesystem::path file_path = curr_path / "file"; //path支持重载/运算符
if(boost::filesystem::exists(file_path)) { //判断路径是否存在
std::string strPath = file_path.string();
} else {
boost::filesystem::create_directory(file_path); //文件夹不存在则创建一个
boost::filesystem::create_directories(file_path);
}
bool bIsDirectory = boost::filesystem::is_directory(file_path); //推断file_path是否为文件夹
boost::filesystem::recursive_directory_iterator beg_iter(file_path);
boost::filesystem::recursive_directory_iterator end_iter;
for (; beg_iter != end_iter; ++beg_iter) {
if (boost::filesystem::is_directory(*beg_iter)) {
continue;
} else {
std::string strPath = beg_iter->path().string(); //遍历出来的文件名称
}
}
boost::filesystem::path new_file_path = file_path / "test.txt";
if(boost::filesystem::is_regular_file(new_file_path)) { //推断是否为普通文件
unsigned long int sizefile = boost::filesystem::file_size(new_file_path); //文件大小(字节)
}
boost::filesystem::remove(new_file_path);//删除文件new_file_path
// recusively copy file or directory from $src to $dst
void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst) {
if (!boost::filesystem::exists(dst)) {
boost::filesystem::create_directories(dst);
}
for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it) {
const boost::filesystem::path newSrc = src / it->path();
const boost::filesystem::path newDst = dst / it->path();
if (boost::filesystem::is_directory(newSrc)) {
CopyFiles(newSrc, newDst);
} else if (boost::filesystem::is_regular_file(newSrc)) {
boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
} else {
_ftprintf(stderr, _T("Error: unrecognized file - %s"), newSrc.string().c_str());
}
}
}
bool CopyDirectory(const std::string &strSourceDir, const std::string &strDestDir)
{
boost::filesystem::recursive_directory_iterator end; //设置遍历结束标志,用recursive_directory_iterator即可循环的遍历目录
boost::system::error_code ec;
for (boost::filesystem::recursive_directory_iterator pos(strSourceDir); pos != end; ++pos) {
//过滤掉目录和子目录为空的情况
if (boost::filesystem::is_directory(*pos))
continue;
std::string strAppPath = boost::filesystem::path(*pos).string();
std::string strRestorePath;
//replace_first_copy在algorithm/string头文件中,在strAppPath中查找strSourceDir字符串,找到则用strDestDir替换,替换后的字符串保存在一个输出迭代器中
boost::algorithm::replace_first_copy(std::back_inserter(strRestorePath), strAppPath, strSourceDir, strDestDir);
if (!boost::filesystem::exists(boost::filesystem::path(strRestorePath).parent_path())) {
boost::filesystem::create_directories(boost::filesystem::path(strRestorePath).parent_path(), ec);
}
boost::filesystem::copy_file(strAppPath, strRestorePath, boost::filesystem::copy_option::overwrite_if_exists, ec);
}
if (ec) {
return false;
}
return true;
}
boost::filesystem::unique_path()
boost::filesystem::remove_all(directory);
boost::filesystem::path path = "/tmp/test";
path += ".txt";
boost::filesystem::path tmp_dir = boost::filesystem::temp_directory_path();
boost::filesystem::rename(src_path, dst_path);
time_t t = boost::filesystem::last_write_time(path);
if (boost::filesystem::exists(dir_path) && !boost::filesystem::is_directory(dir_path)) {}
boost::system::error_code error_code;
if (!boost::filesystem::create_directories(dir_path, error_code)) {
std::cout << "Fail to create directory: ", error_code.message()));
}
boost::filesystem::is_regular_file(path, error_code);
#include <filesystem>
std::filesystem::is_regular_file()
std::filesystem::path path(topic);
const std::string cam_name = *(++path.begin());
CMakeLists:
cmake_minimum_required(VERSION 2.8.3)
project(test)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
find_package(Boost REQUIRED COMPONENTS
filesystem
)
include_directories(
${Boost_INCLUDE_DIRS}
)
add_executable(test main.cc)
target_link_libraries(test ${Boost_LIBRARIES})