Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
这是简单的使用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文件系统还不错
要是再加入对压缩包或者资源包的支持就更好了!

posted on 2010-08-11 16:42 ccsdu2009 阅读(743) 评论(0)  编辑 收藏 引用 所属分类: boost库等

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理