Advertisement
Kimossab

Dirent attempt

Apr 21st, 2016
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. /* list contents of a directory */
  2. #include <iostream>
  3. #include "C:\\Users\\Kimossab\\Documents\\Visual Studio 2015\\Projects\\test\\test\\dirent.h"
  4.  
  5. using namespace std;
  6.  
  7. void main(int argc, char *argv[])
  8. {
  9.     /* check command line arguments */
  10.     if (argc <= 2)
  11.     {
  12.         fprintf(stderr, "usage: printdir directory\n");
  13.         return;
  14.     }
  15.  
  16.     /* print contents of directories listed in command line */
  17.     /* open directory stream */
  18.     DIR *dir = opendir(argv[1]);
  19.     DIR *dir2;
  20.     struct dirent *ent;
  21.     struct dirent *ent2;
  22.     string aux;
  23.  
  24.     if (dir != NULL)
  25.     {
  26.         /* print all the files and directories within directory */
  27.         while ((ent = readdir(dir)) != NULL)
  28.         {
  29.             switch (ent->d_type)
  30.             {
  31.                 case DT_DIR:
  32.                     string ax = argv[1];
  33.                     aux = ax + "\\" + ent->d_name;
  34.                     dir2 = opendir(aux.c_str());
  35.                     if (dir2 != NULL)
  36.                     {
  37.                         /* print all the files and directories within directory */
  38.                         while ((ent2 = readdir(dir2)) != NULL)
  39.                         {
  40.                             switch (ent->d_type)
  41.                             {
  42.                                 case DT_REG:
  43.                                     cout << ent->d_name;
  44.                             }
  45.                         }
  46.                         cout << ";";
  47.                     }
  48.                     break;
  49.             }
  50.         }
  51.  
  52.         closedir(dir);
  53.     }
  54.     else
  55.         /* could not open directory */
  56.         perror("");
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement