2016-07-27

C: Simple Walk Through Directory Contents

You can easily walk through the entries in a directory. Note, that it's unordered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
 
void walk_dir(const char *path)
{
 struct dirent *entry;
 DIR *dir = opendir(path);
 
 while ((entry = readdir(dir)) != NULL) {
  printf("Entry: %s\n", entry->d_name);
 }
}
 
int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

No comments :

Post a Comment