Question: c programming - can anyone explain to me how does the following code work recursively? I highlighted the parts that I don't quit understand. The
c programming - can anyone explain to me how does the following code work recursively? I highlighted the parts that I don't quit understand.
The code should be able to print out all of directory and its subdirectories recursively
#include
#include
void listdir(const char *name)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
if (entry->d_type == DT_DIR) {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name); // what exactly does snprintf do?
path[len] = 0; // what does this statement mean? why set it to 0?
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
printf("%s/%s ", name, entry->d_name);
listdir(path); //How does it been called recursively
}
else
printf("%s/%s ",name, entry->d_name);
} while (entry = readdir(dir));
closedir(dir);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
