Welcome 微信登录

首页 / 操作系统 / Linux / Linux下Gtkmm枚举目录中所有文件(包含子目录)

Linux下Gtkmm枚举目录中所有文件(包含子目录),使用glibmm库
  1. #include <gtkmm.h>   
  2. #include <glibmm.h>   
  3. #include <giomm.h>   
  4. #include <iostream>   
  5. #include <string>   
  6. #include <vector>   
  7. using namespace Gtk;   
  8. using namespace Glib;   
  9. using namespace std;   
  10. void EnumDir(string rootpath,vector<Glib::ustring>& vString)   
  11. {   
  12.     Glib::Dir dirs(rootpath);   
  13.     std::list<std::string> entries (dirs.begin(), dirs.end());   
  14.     for(std::list<std::string>::iterator  dirItr=entries.begin();dirItr!=entries.end();dirItr++)   
  15.     {   
  16.         //使用流输出Glib::ustring一定要.c_str()或.raw()   
  17.         //不然可提示:"terminate called after throwing an instance of "Glib::ConvertError"".   
  18.         Glib::ustring fullpath=rootpath+"/"+*dirItr;   
  19.         //cout<<fullpath.c_str()<<"--------"<<endl;   
  20.         //cout<<fullpath.raw()<<"#######"<<endl;   
  21.         vString.push_back(fullpath);   
  22.         try  
  23.         {   
  24.             //Glib::file_test(fullpath,Glib::FILE_TEST_IS_DIR | Glib::FILE_TEST_EXISTS)   
  25.             //上述表达式表示:如果fullpath为目录或文件存在均返回真   
  26.             //Glib::file_test(fullpath,Glib::FILE_TEST_EXISTS & Glib::FILE_TEST_IS_DIR)   
  27.             //上述表达式表示:如果fullpath为目录且目录存在才返回真,还过这样写没必要,Glib::FILE_TEST_IS_DIR   
  28.            if (Glib::file_test(fullpath,Glib::FILE_TEST_IS_DIR))   
  29.             {   
  30.                 EnumDir(fullpath,vString);   
  31.             }   
  32.         }   
  33.         catch(Glib::FileError er)   
  34.         {   
  35.             cout<<"Error:"<<fullpath.c_str()<<":"<<er.what().c_str() <<endl;   
  36.         }   
  37.     }   
  38. }   
  39. int main(int argc,char* argv[])   
  40. {   
  41.     vector<Glib::ustring> files;   
  42.     EnumDir("/home/yanxiang/桌面",files);   
  43.     for(vector<Glib::ustring>::iterator itr=files.begin();itr!=files.end();itr++)   
  44.     {   
  45.         cout<<(*itr).c_str()<<"********"<<endl;   
  46.     }   
  47.     return 0;   
  48. }  
iReport部署到Linux系统字体问题研究Linux分页技术碰到著名的局部性原理相关资讯      Linux教程  Gtkmm 
  • Linux教程:如何在命令行中查看目  (07/28/2014 12:22:23)
  • Linux 修改root密码  (11/03/2012 07:53:38)
  • su - root 与su root的区别  (06/06/2012 00:39:40)
  • Linux进程间通信:消息队列  (01/28/2013 09:43:00)
  • U盘安装Linux开机无法启动解决方法  (10/07/2012 08:55:52)
  • Windows 7/Linux 同步时间  (05/15/2012 06:17:55)
本文评论 查看全部评论 (1)
表情: 姓名: 字数


评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的