Advertisement
Guest User

Count files

a guest
Dec 12th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(int argc, char* argv[], char* envp[])
  7. {
  8.     int file_count = 0, symb_links = 0;
  9.     DIR *dirp;
  10.     struct dirent *entry;
  11.  
  12.     if (argc != 2)
  13.     {
  14.         printf("invalid input!\n");
  15.         exit(-1);
  16.     }
  17.  
  18.     dirp = opendir(argv[1]);
  19.     if (dirp == NULL){
  20.         printf("Can't open the directory!\n");
  21.         exit(-1);
  22.     }
  23.  
  24.     while ((entry = readdir(dirp)) != NULL) {
  25.         if (entry->d_type != DT_DIR) {
  26.              file_count++;
  27.         }
  28.     }
  29.     closedir(dirp);
  30.  
  31.     printf("Number of files = %d\n", file_count);
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement