Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:58:07+00:00 2026-05-25T21:58:07+00:00

This program is printing the directories at the root level Directory_1 Directory_2 but I

  • 0

This program is printing the directories at the root level

Directory_1
Directory_2

but I want to be able to print the directories within them too

Directory_1
   Directory_1_2
   Directory_1_3
Directory_2
   Directory 2_1
      Directory_2_1_1
Directory_4

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?

Here is my output

..
.
Directory_1
Directory_2
Failed to open directory: No such file or directory

Code

#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>

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->d_name);
   arg_tmp = direntp->d_name;
  }    
   printDepthFirst(arg_tmp);

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

}

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..

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:58:07+00:00Added an answer on May 25, 2026 at 9:58 pm

    Well this should help:

    #define _XOPEN_SOURCE 500
    #include <ftw.h>
    #include <stdio.h>
    
    static int display_info(const char *fpath, const struct stat *sb,
                 int tflag, struct FTW *ftwbuf)
    {
        switch(tflag)
        {
            case FTW_D:
            case FTW_DP: puts(fpath); break;
        }
        return 0; /* To tell nftw() to continue */
    }
    
    int main(int argc, char *argv[])
    {
        if (argc != 2) {
            fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
            return 1; 
        }  
    
        int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
    
        if (nftw(argv[1], display_info, 20, flags) == -1)
        {
            perror("nftw");
            return 255;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program, which prints a sine wave but it's too fast, how
In this below program i am printing the contents of the div when i
I wrote this program to remove a node from tree, but it still exists!
I want to write a program which is capable of printing out system properties.
Hi m using this program to print my files bt it opens the file
In this program, I am reading key.pc.db file and printing its mid value. #include
In this bellow program if you click on 'PRINT', the html content of will
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.