文件操作
对文件的操作是代码中经常遇到的问题。尤其是常用的判断文件对否存在等。boost提供部分操作接口,极大的方便了需求。首先需要加入system
与filesystem
库:
target_link_libraries(boostTest boost_filesystem boost_system)
1.路径
- 基本路径操作
父路径:parent_path()
,文件名称:filename()
,文件系统的路径表示:directory_string()
void pathTest()
{
char str[] = "the path is (/root).";
path p(str + 13, str + 14);
assert(!p.empty());
p /= "etc";
string filename = "test.conf";
// path重载了/=函数
p /= filename;
cout << p << endl;
path p2("/usr/local/include/xxx.hpp");
cout << p2.string() << endl;
cout<<p2.directory_string()<<endl;
cout << p2.parent_path() << endl;
cout << p2.stem() << endl;
//得到文件名称
cout << p2.filename() << endl;
path p3 = "/boost/tools/libs";
BOOST_AUTO(pos, p3.begin());
while (pos != p3.end())
{
cout << "[" << *pos << "]";
++pos;
}
cout << endl;
// cout<<system_complete(p)<<endl;
}
输出为:
"/etc/test.conf"
/usr/local/include/xxx.hpp
"/usr/local/include"
"xxx"
"xxx.hpp"
["/"]["boost"]["tools"]["libs"]
- 路径属性
路径状态包括:路径是否存在,是否为路径,占用空间等。
文件的当前路径:current_path()
是否为路径:is_directory
void pathStatusTest()
{
//判断文件属性状态,是否存在
if (status("dev/null").type() == file_not_found)
{
cout << "file is not found" << endl;
}
cout << status("/dev/null").type() << endl;
//获得当前的路径
cout << "current_path is:" << current_path() << endl;
const int GBTYPES = 1024 * 1024 * 1024;
//space_info 得到该路径下的磁盘空间分配情况
space_info si = space("/root/project/monitor_center/dbhelper_server/");
cout << si.capacity / GBTYPES << endl; //占用的大小
cout << si.available / GBTYPES << endl; //可用空间
cout << si.free / GBTYPES << endl; //剩余空间
//判断是否为路径
if (is_directory("/root"))
{
cout << "it is a directory!" << endl;
}
//判断路径是否存在
if (!exists("/root/project"))
{
cout << "root/project is a directory!" << endl;
}
}
输出为:
file is not found
6
current_path is:"/root/project/Test/boostTest/build"
133
123
123
it is a directory!
- 路径操作进阶
void pathOperTest()
{
namespace fs = boost::filesystem;
path ptest = "/root/pathTest";
if (exists(ptest)) //判断路径是否存在
{
cout << "path is exists!" << endl;
//判断目录中是否有文件
if (boost::filesystem::is_empty(ptest))
{
cout << "start remove path!" << endl;
//删除空目录或者该目录下的文件
fs::remove(ptest);
}
else
{
cout << "remove path file -R" << endl;
//递归删除文件,包括文件夹中的子文件夹
remove_all(ptest);
}
}
assert(!exists(ptest));
if (exists("/root/path.txt")) //判断文件是否存在
{
cout << "check file exists" << endl;
}
//创建路径
create_directory(ptest);
}
- 迭代路径下文件
遍历路径下面的所有文件,分为两种方式,一种是仅仅遍历当前文件夹下的文件,不包括子文件夹中的文件:directory_iterator
,2)递归的遍历文件夹中的所有文件:recursive_directory_iterator
void recursive_dir(const path &dir)
{
//遍历目录下的所有文件
directory_iterator end; //directory_iterator只能迭代本层目录,无法深度遍历目录
for (directory_iterator pos(dir); pos != end; ++pos)
{
if (is_directory(*pos))
{
//实现递归遍历路径中的文件
recursive_dir(*pos);
}
else
{
cout << *pos << endl;
}
}
}
void recursive_dir2(const path &dir)
{
//可以深度遍历文件
typedef recursive_directory_iterator rd_iterator;
rd_iterator end;
for (rd_iterator pos(dir); pos != end; pos++)
{
// cout<<"level"<<pos.level()<<":"<<*pos<<endl;
if (is_directory(*pos))
{
pos.no_push();
}
else
{
cout << *pos << endl;
}
}
}