Advertisement
Mark2020H

Solution for finding file with set extension in directory

Feb 26th, 2020
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. /* You will need to include the following headers on your C++ class declaration file  the all works
  2. *
  3. *Info on this  can be found at this address  http://www.gnu.org/software/libc/manual/html_node/
  4. * Reading_002fClosing- Directory.html#Reading_002fClosing-Directory
  5. */
  6.  
  7.  
  8. #include <dirent.h>
  9. #include <sys/types.h>
  10. #include <cstring>
  11.  
  12.  
  13. void ReadFile::listFiles(const char* dirname)
  14. {
  15.     DIR *di;
  16.     char *ptr1,*ptr2;
  17.     int retn;
  18.     struct dirent *dir;
  19.     di = opendir(dirname); //specify the directory name
  20.     if (di)
  21.     {
  22.         while ((dir = readdir(di)) != NULL)
  23.         {
  24.             ptr1=strtok(dir->d_name,dirname);
  25.             ptr2=strtok(NULL,".");
  26.             if(ptr2!=NULL)
  27.             {
  28.                 retn=strcmp(ptr2,"txt");
  29.                 if(retn==0)
  30.                 {
  31.                     printf("%s\t",ptr1);
  32.                 }
  33.             }
  34.         }
  35.         closedir(di);
  36.     }
  37.  
  38.  
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement