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 7430577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:09:49+00:00 2026-05-29T09:09:49+00:00

I thought this would be pretty academic but its not. I’m trying to traverse

  • 0

I thought this would be pretty academic but its not. I’m trying to traverse a directory try (recursively). However, I keep getting the following message when calling readdir():

 ./xsed.bin
 make: *** [run] Segmentation fault

GDB has the following output when tracing though the program:

Breakpoint 1, recurseDir (path=0x401090 ".") at xsed.c:216
216             DIR *currD = opendir(path);
(gdb) n
217             if( currD = NULL)
(gdb) n
226                     entry = readdir(currD);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7afef86 in __readdir (dirp=0x0) at ../sysdeps/unix/readdir.c:45
45      ../sysdeps/unix/readdir.c: No such file or directory.
    in ../sysdeps/unix/readdir.c

I’ve read several places never to call readdir() twice on the same Dir* object. I’m getting a segfault when calling it once. I’ve checked the path string to ensure its correct (e.g. using “.” or “./” etc)– no luck.

Here’s the code below:
Called like: recurseDir(getcwd(currD,256)) or recurseDir(“.”);

  void recurseDir(char *path)
  {

    DIR *currD = opendir(path);
    if( currD = NULL)
    {
            fprintf(stderr,"Could not open directory: %s\n",path);
            return;
    }
    else
    {
            struct dirent *entry;
            char *dName;
            entry = readdir(currD); <--- segfault

            if( entry == NULL)
            {
                    fprintf(stderr,"Error reading directory.\n");
                    return;
            }

            dName = entry->d_name;
            printf("Current dir:%s\n",dName);

      }
   }

I have no idea how the argument (dirp) is getting to be NULL inside the call to readdir(). NOTE: This is happening during the first call to recurseDir(), no recursion happens at all. I’m I off or is this a compiler/machine thing?

Please help!!!!

  • 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-29T09:09:50+00:00Added an answer on May 29, 2026 at 9:09 am

    For one thing, you have if( currD = NULL). You probably meant == rather than =. Yep, that would definitely explain why your currD is NULL. 🙂

    By the way, some people prefer to write NULL == currD to guard against this. Or you could just say !currD.

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

Sidebar

Related Questions

I thought this would be pretty simple, but guess not. I have the following
I thought this would be pretty easy but I'm running into all sorts of
OK so I thought this would be pretty simple but it's tripping me up..
Okay at first I thought this would be pretty straightforward. But I can't think
I thought this would be fairly simple but it turns out not to work
Thought this would be pretty straight forward, but my value is remaining the same
I thought this would be pretty straightforward, but I've been searching for a way
I thought this would be pretty simple, but it is proving a bit more
I thought this would be pretty simple to do but seems I must be
I'm new to AutoFac, but I thought this would be pretty straight forward. I've

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.