Hello i want to create a file crawler that lists all files in hard disk using directory functions in C i have been able to list files in one directory only and haven't been able to crawl through other directories and sub-directories this is what i have so far.

char workingDir[100];
    getcwd(workingDir, 100);
    printf("Working Dir \t%s\n", workingDir);
    //Change dirctory to etc
    getcwd(workingDir, 100);
    printf("Changed to \t%s\n", workingDir);
    //Dirent stuff
    DIR *directory = opendir(workingDir);
    struct dirent *dirs;
    printf("Directories in %s\n",workingDir);
   while((dirs = readdir(directory))!='\0')
       printf("Dir Name: %s\n\tDir No: %d\nDir Type: %u\n",dirs->d_name,dirs->d_ino,dirs->d_type);
6 Years
Discussion Span
Last Post by Madawar

Check this thread out -- it was written in c++ but could be easily ported to C. If you read the code you will discover that it uses recursion to crawl though all sub directories.

Edited by Ancient Dragon: n/a


Hi,Dragon i don't know much of C++ but i guess i can deduce part of the code does

vector<string> LIST;

mean that the position of the next readdir() call in
the directory stream are stored in a list or array of some sort and should i use an array to store the next readdir() call in C code?


Ignore the vector part and concentrate on how it recognizes a new sub directory and makes a recursive call to process the files in it. In C language I would replace that vector with a linked list if you need to keep the list of all files in memory for some reason.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.