2016-07-27

C: Sorted Walk Through Directory Contents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
 
void walk_dir(const char *path)
{
 struct dirent **namelist;
 int i, n;
 
 n = scandir(path, &namelist, NULL/*filter*/, alphasort);
 if (n < 0)
 {
  perror("scandir");
 } else {
  for (i = 0; i < n; i++) {
   printf("Entry: %s\n", namelist[i]->d_name);
   free(namelist[i]);
  }
  free(namelist);
 }
}
 
int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

No comments :

Post a Comment