daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1.glob
  2. ------
  3. ```c++
  4. #include <glob.h>
  5.  
  6. glob_t glob_result;
  7. glob("/your_directory/*",GLOB_TILDE,NULL,&glob_result);
  8. for(unsigned int i=0; i<glob_result.gl_pathc; ++i){
  9.   cout << glob_result.gl_pathv[i] << endl;
  10. }
  11. globfree(&glob_result);
  12. ```
  13.  
  14. **vector封装:**
  15. ```c++
  16. #include <glob.h>
  17. #include <vector>
  18. #include <string>
  19.  
  20. inline std::vector<std::string> glob(const std::string& pat){
  21.     using namespace std;
  22.     glob_t glob_result;
  23.     glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
  24.     vector<string> ret;
  25.     for(unsigned int i=0;i<glob_result.gl_pathc;++i){
  26.         ret.push_back(string(glob_result.gl_pathv[i]));
  27.     }
  28.     globfree(&glob_result);
  29.     return ret;
  30. }
  31. ```
  32.  
  33.  
  34. 2.c++17
  35. -------
  36. ```c++
  37. #include <string>
  38. #include <iostream>
  39. #include <filesystem>
  40. namespace fs = std::filesystem;
  41.  
  42. int main()
  43. {
  44.     std::string path = "path_to_directory";
  45.     for (auto & p : fs::directory_iterator(path))
  46.         std::cout << p << std::endl;
  47. }
  48. ```
  49.  
  50.  
  51. 3.boost
  52. -------
  53. ```c++
  54. #include <string>
  55. #include <iostream>
  56. #include <boost/filesystem.hpp>
  57. using namespace std;
  58. using namespace boost::filesystem;
  59.  
  60. int main()
  61. {
  62.     path p("D:/AnyFolder");
  63.     for (auto i = directory_iterator(p); i != directory_iterator(); i++)
  64.     {
  65.         if (!is_directory(i->path())) //we eliminate directories
  66.         {
  67.             cout << i->path().filename().string() << endl;
  68.         }
  69.         else
  70.             continue;
  71.     }
  72. }
  73. ```
  74.  
  75.  
  76. 4. dirent
  77. -------
  78. ```c++
  79. #include <dirent.h>
  80.  
  81. DIR *dir;
  82. struct dirent *ent;
  83. if ((dir = opendir ("c:\\src\\")) != NULL) {
  84.   /* print all the files and directories within directory */
  85.   while ((ent = readdir (dir)) != NULL) {
  86.     printf ("%s\n", ent->d_name);
  87.   }
  88.   closedir (dir);
  89. } else {
  90.   /* could not open directory */
  91.   perror ("");
  92.   return EXIT_FAILURE;
  93. }
  94. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top