Life & Code

代码是咒语,我是魔法师

boost::filesystem的文件遍历

 

#include  " boost/filesystem/operations.hpp "
#include 
" boost/filesystem/path.hpp "
#include 
< iostream >
using   namespace  std;
namespace  fs  =  boost::filesystem;

void  PrintAllFile(fs::path  & full_path);



int  main(  int  argc,  char *  argv[] )
{

    fs::path full_path( 
" D:\\ "  ,fs::native);
    PrintAllFile(full_path);
}

void  PrintAllFile(fs::path  & full_path)
{
 
if (fs::exists(full_path))
 
{
  fs::directory_iterator item_begin(full_path);
  fs::directory_iterator item_end;
  
for ( ;item_begin   !=  item_end; item_begin ++ )
  
{
   
   
if (fs::is_directory( * item_begin))
   
{
    cout 
<< item_begin -> native_file_string() << " \t[dir] " << endl;
    PrintAllFile(
* item_begin);
   }

   
else
   
{
    cout 
<< item_begin -> native_file_string() << endl;
   }

  }

 }

}

posted on 2006-12-13 00:29 橙子 阅读(5968) 评论(3)  编辑 收藏 引用 所属分类: C++ & STL

评论

# re: boost::filesystem的文件遍历 2007-09-21 15:39 jazz

我也刚学这个 贴个自己的例子

#include <iostream>
#include <string>
#include <ctime>
#pragma warning(push)
#pragma warning(disable:4819)
#pragma warning(disable:4996)
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem.hpp>
#pragma warning(pop)

using namespace std;
using namespace boost;
namespace fs = boost::filesystem;

void RecusiveListFiles(fs::path& fpath)
{
fs::recursive_directory_iterator beg_iter(fpath);
fs::recursive_directory_iterator end_iter;
for (; beg_iter != end_iter; ++beg_iter)
{
if (fs::is_directory(*beg_iter))
{
continue;
}
else
{
cout<<beg_iter->path().file_string()<<'\n';

time_t t = fs::last_write_time(*beg_iter);
cout<<ctime(&t);
uintmax_t filesize = fs::file_size(*beg_iter);
cout<<filesize<<"\n\n";
}
}
}

int main(int argc, char* argv[])
{
try
{
if (argc < 2)
{
cout<<"Invalid arg --- You should input a Directory path as an argument.(ex: c:\\DELL)\n";
return -1;
}
RecusiveListFiles(fs::path(string(argv[1]), fs::native));
}
catch (exception& e)
{
cout<<e.what()<<endl;
}

return 0;
}  回复  更多评论   

# re: boost::filesystem的文件遍历 2008-12-29 00:46 Lingol

fs::directory_iterator item_end;

为何不需初始化?  回复  更多评论   

# re: boost::filesystem的文件遍历 2013-12-21 17:42 shit_engineer

你确定这代码能跑编过?  回复  更多评论   


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


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(10)

随笔分类

随笔档案

相册

收藏夹

搜索

最新评论

阅读排行榜