filesystem库是一个可移植的文件系统操作库,使用POSIX标准表示文件系统路径,使得C++具有了类似于脚本语言的功能,可以跨平台操作目录、文件,写出通用脚本。
使用filesystem组件,需要包含头文件 <boost/filesystem.hpp> 即: #include <boost/filesystem.hpp> using namespace boost::filesystem;
01 -- path类
路径表示
char str[] = 'the path is (/root).'; // 设置字符串“the path is (/root).” str[0] = 't', str[1] = 'h', ... , str[13] = '/', ... , str[17] = 't', str[18] = ')', ...
path p(str + 13, str + 14); // 取字符串str中的 str[13], 截取方式是 start_str = str[13], end_str = str[14], new_str = str[13], 则 p = '/'
assert(!p.empty()); // 判断p是否为空
p /= ''etc; // 符号‘/=’是字符串连接,类似于python中的 ‘+’, 得到新字符串为: ‘/etc’; 具有相同操作的还有 += 符号,但是不加路径分隔符
string filenam