歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux下Gtkmm枚舉目錄中所有文件(包含子目錄)

Linux下Gtkmm枚舉目錄中所有文件(包含子目錄)

日期:2017/2/28 16:13:16   编辑:Linux教程

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. }
Copyright © Linux教程網 All Rights Reserved