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

  • Home
  • SEARCH
  • 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 8976089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:02:31+00:00 2026-06-15T19:02:31+00:00

I am trying to traverse through a directory whose path is given but the

  • 0

I am trying to traverse through a directory whose path is given but the following program is not going inside the directories and is stuck in infinit loop. What is the problem in this code:

void func(char path[]);
int main(int argc, char *argv)
{
    char buf[255];
    getcwd(buf,255);
    func(buf);
}

void func(char path[])
{
    DIR *dirp;
    struct stat states;
    struct dirent *direntp;

    dirp=opendir(path);
    chdir(path);

    while((direntp=readdir(dirp))!=NULL)
    {
        if(S_ISDIR(states.st_mode))//true than returns zero
        {
            printf("%s\n",direntp->d_name);
        }
    else
        if(!(strcmp(direntp->d_name,".")||strcmp(direntp->d_name,"..")))// if true retrns zero
        {   
            continue;
        }
        else
        {
            func(direntp->d_name);
            chdir("..");
        }
    }
}
  • 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-06-15T19:02:33+00:00Added an answer on June 15, 2026 at 7:02 pm

    You can check whether if the node is directory without an auxiliary states.struct dirent has a field called d_type, AND it with DT_DIR to test if it is a directory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to traverse a given directory and create a list of files
I'm trying to traverse through all the subdirectories of the current directory in Perl,
I thought this would be pretty academic but its not. I'm trying to traverse
I'm trying to write some Python code that will traverse each directory in the
I am trying to use the FindFirstFile function to traverse through all of my
I have written a simple program to traverse through the nodes of a linked
I'm trying to traverse through nested array var, and select those array elements which
I am trying to traverse through a simple sitemap (adding and deleting elements on
I am trying to use xPath to traverse through the code of a newspaper
I am trying to clone an element, and then traverse through it's child elements

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.