一、说明
boost库中的filesystem中有关路径的操作十分的方便特别是path重载的/,看起来就像对普通路径的书写一样,你再也不用担心为组合路径时少写‘/’而导致找不到文件或者程序直接崩溃烦恼啦,因为当你缺少时path会自动的给你添加上啦。而在C++14以上的版本这块正式成了C++的标准啦。废话不说啦,直接上代码,下面只演示了一些常用的,更多方法可以直接go到源码中去看看。
二、测试代码
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main(int argc, char *argv[])
{
string filePath = "/work/test/testPath";
string fileName = "test_path.txt";
//path("/work/test/testPath/test_path.txt");
boost::filesystem::path testFilePath(filePath);
// 使用'/'追加路径,并把路径转化成字符串
string fullFilePath = (testFilePath/fileName).string();
cout << "fullFilePath:" << fullFilePath << endl;
if(!testFilePath.empty())
cout << "path is not empty" << endl;
// 取fullFilePath中的