boost文件系统
- 01.boost::filesystem::path
- boost::filesystem::path 是 Boost.Filesystem 中的核心类,它表示路径的信息,并提供了处理路径的方法
- boost后续文件操作都是从这个开始展开的。
- 02.boost::filesystem::file_status
- 这是一个记录文件信息的类,通过文件信息来进行其他辅助函数的操作。
- filesystem::file_status是filesystem::status的返回值
- is_directtory( )判断是否为一个目录文件(辅助函数)
- is_regular_file( )判断是否为一个普通文件(辅助函数)
//例子:
int main()
{
boost::filesystem::path p("./StuCode");
boost::filesystem::file_status s = boost::filesystem::status(p);
try
{
std::cout<<"is_directory : "<<boost::filesystem::is_directory(s)<<std::endl;;
std::cout<<"is_regular_file : "<<boost::filesystem::is_regular_file(s)<<std::endl;
}
catch(boost::filesystem::filesystem_error &e)
{
std::cerr<<e.what()<<std::endl;
}
return 0;
}
- 03.boost::filesystem::file_size()
- filesystem::file_size(),即返回其文件大小
int main()
{
boost::filesystem::path p("./1.cc");
try
{
std::cout<<"file_size : "<<boost::filesystem::file_size(p)<<std::endl;
}
catch(boost::filesystem::filesystem_error &e)
{
std::cerr<<e.what()<<std::endl;
}
return 0;
}
- 04.file_last_write_time()
- .file_last_write_time()用来返回最后一次修改时间
int main()
{
boost::filesystem::path p("./1.cc");
try
{
std::cout<<"last_write_time : "<<boost::filesystem::last_write_time(p)<<std::endl;
}
catch(boost::filesystem::filesystem_error &e)
{
std::cerr<<e.what()<<std::endl;
}
return 0;
}
- 05.boost::filesystem::space()
- 用来显示磁盘占用空间和可用空间
- capacity,free ,available为其属性
int main()
{
boost::filesystem::path p("./1.cc");
boost::filesystem::space_info info = boost::filesystem::space(p);
try
{
std::cout<<"capacity : "<<info.capacity<<std::endl;
std::cout<<"free : "<<info.free<<std::endl;
std::cout<<"available : "<<info.available<<std::endl;
}
catch(boost::filesystem::filesystem_error &e)
{
std::cerr<<e.what()<<std::endl;
}
return 0;
}
- 06.文件和目录的删除,改名,创建操作
- boost::filesystem::create_directory( file_status )用来创建目录
- boost::filesystem::rename( path, newName)更改文件名
- boost::filesystem::remove 删除目录
- copy_file复制文件
- copy_files复制目录
int main()
{
boost::filesystem::path p("./Dir");
boost::filesystem::path p0("./Dir0");
boost::filesystem::path p1("1.cc");
boost::filesystem::path p2("2.cc");
try
{
if(boost::filesystem::create_directory(p))
{
boost::filesystem::file_status s =boost::filesystem::status(p);
std::cout<<boost::filesystem::is_directory(s)<<std::endl;
}
//删除目录
boost::filesystem::remove(p);
boost::filesystem::file_status s =boost::filesystem::status(p);
std::cout<<boost::filesystem::is_directory(s)<<std::endl;
//拷贝文件
boost::filesystem::copy_file(p1,p2);
s =boost::filesystem::status(p2);
std::cout<<boost::filesystem::exists(s)<<std::endl;
}
catch(boost::filesystem::filesystem_error &e)
{
std::cerr<<e.what()<<std::endl;
}
return 0;
}
以上均参考boost中文文档,如有错误可以私信我,这里表示感谢!