2016-07-27

C: Simple Walk Through Directory Contents

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

#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