How to list all subdirectories in a given directory in C?
Is there a way to list all subdirectories in a given directory path in C? I was hoping I would be able to do it with the stat() function but it only works on files. CodingLab stat works on directories too. #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> int num_dirs(const char* path) { int dir_count = 0; struct dirent* dent; DIR* srcdir = opendir(path); if (srcdir == NULL) { perror("opendir"); return -1; } while((dent = readdir(srcdir)) != NULL) { struct stat st; if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; if (fstatat