Printing all the directories using c++
<p>This program is printing the directories at the root level</p>

<pre><code>Directory_1
Directory_2
</code></pre>

<p>but I want to be able to print the directories within them too</p>

<pre><code>Directory_1
   Directory_1_2
   Directory_1_3
Directory_2
   Directory 2_1
      Directory_2_1_1
Directory_4
</code></pre>

<p>I am trying to do it recursively but I am finding it hard to pass the Directory_1 as a root so it gets evaluated.. What am i missing?</p>

<p>Here is my output</p>

<pre><code>..
.
Directory_1
Directory_2
Failed to open directory: No such file or directory
</code></pre>

<p>Code</p>

<pre><code>#include &lt;dirent.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdio.h&gt;
#include &lt;sys/stat.h&gt;

char *arg_temp;

int printDepthFirst(char *arg_tmp);

int main(int argc, char *argv[]) {

   if (argc != 2) {
      fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
      return 1; 
   }  


  arg_temp = argv[1]; 

  printDepthFirst(arg_temp);

}

int printDepthFirst(char *arg_tmp)
{

   struct dirent *direntp;
   DIR *dirp;

   if ((dirp = opendir(arg_tmp)) == NULL) {
      perror ("Failed to open directory");
      return 1;
   }  


   while ((direntp = readdir(dirp)) != NULL)
  {
   printf("%s\n", direntp-&gt;d_name);
   arg_tmp = direntp-&gt;d_name;
  }    
   printDepthFirst(arg_tmp);

  while ((closedir(dirp) == -1) &amp;&amp; (errno == EINTR)) ;
     return 0;

}
</code></pre>

<p>Now, I know some people get irritated when asking questions that they think I am expecting them to code this, you dont need to, if you can tell me theoretically what i need to do.. I will research it although if its a small programmatically fix and you can post that I would really appreaciate it.. but if not.. I would also love to hear about what needs to be done in words..</p>

<p>Thank you</p>
