这是简单的使用boost文件系统的例子
在devc++下编译需要2个库一个libboostsystem一个libboostfilesystem
基本的研发如下:
#include <cstdlib>
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char *argv[])
{
std::cout<<"sizeof(intmax_t)is:"<<sizeof(boost::intmax_t)<<'\n';
fs::path path("main.cpp",fs::native);
if(!fs::exists(path))
{
return -1;
}
if(!fs::is_regular(path))
return -1;
std::cout<<"size is:"<<fs::file_size(path)<<"字节"<<std::endl;
fs::path full_path(fs::initial_path<fs::path>());
full_path = fs::system_complete(fs::path("main.cpp"));
//! 获取给定文件全路径
cout<<"full path name is:"<<full_path.file_string()<<std::endl;
//! 检测是不是路径
cout<<"is path:"<<fs::is_directory(full_path)<<std::endl;
unsigned long file_count = 0;
unsigned long dir_count = 0;
unsigned long other_count = 0;
unsigned long err_count = 0;
full_path = fs::system_complete(fs::path("C:\\WINDOWS"));
//! 目录迭代
fs::directory_iterator end_iter;
for(fs::directory_iterator dir_itr(full_path);dir_itr != end_iter;++dir_itr)
{
try
{
if(fs::is_directory(dir_itr->status()))
{
++dir_count;
std::cout<<dir_itr->path().filename()<<"[directory]\n";
}
else if(fs::is_regular_file(dir_itr->status()))
{
++file_count;
std::cout<<dir_itr->path().filename()<<"\n";
//! 全路径名字
std::cout<<dir_itr->path().native_file_string()<<std::endl;
}
else
{
++other_count;
std::cout << dir_itr->path().filename() <<"[other]\n";
}
}
catch(const std::exception & ex)
{
++err_count;
std::cout<<dir_itr->path().filename()<<" "<<ex.what()<<std::endl;
}
}
std::cout<<"\n"<<file_count<<"files\n"<<dir_count<<" directories\n"<<other_count<<" others\n"<<err_count<<" errors\n";
//! 生成文件目录
fs::create_directories(fs::path("filesystem"));
system("PAUSE");
return EXIT_SUCCESS;
}
//! ccsdu2004
boost文件系统还不错
要是再加入对压缩包或者资源包的支持就更好了!