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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:59:41+00:00 2026-06-12T04:59:41+00:00

I am trying to list all files in a directory recursively. But my code

  • 0

I am trying to list all files in a directory recursively. But my code crashes without giving any error. When the given file is directory I recursively call the function or else print its name.

I am using dirent.h

int list_file(string path)
{
    DIR *dir;
    struct dirent *ent;
    char  *c_style_path;
    c_style_path = new char[path.length()];
    c_style_path = (char *)path.c_str();
    dir = opendir (c_style_path);
    if (dir != NULL) {

        /* print all the files and directories within directory */
        while ((ent = readdir (dir)) != NULL) {
            if(ent->d_type == DT_DIR && (strcmp(ent->d_name,".")!=0) && (strcmp(ent->d_name,"..")!=0))
            {
                string tmp = path + "\\" + ent->d_name;
                list_file(tmp);
            }
            else
            {
                cout<<ent->d_name<<endl;
            }
        }
        closedir (dir);
    } else {
        /* could not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
    delete [] c_style_path;
    return 0;
}

I am not getting as to what I am doing wrong here. Any clues ?

  • 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-12T04:59:42+00:00Added an answer on June 12, 2026 at 4:59 am

    This is the cause:

    c_style_path = (char *)path.c_str();
    //...
    delete[] c_style_path;
    

    as it is delete[]ing memory that it should not and probably results in a double free when path goes out of scope at the end of the function.

    Just use path.c_str() when you require const char*:

    dir = opendir (path.c_str());  
    

    Note that storing the pointer returned by std::string::c_str() is quite dangerous as it can easily lead to a dangling pointer if the std::string instance goes out of scope.

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

Sidebar

Related Questions

I am trying to get a list of all files in a directory by
I have the following code snippet. I'm trying to list all the files in
I'm trying to get a list of all mp3 files in a directory (the
I'm trying to list all files in a virtual directory and it's sub directories.
I am trying to list all the files in a certain directory on a
I am trying to get a list of all files in a directory and
I'm trying to generate a text file that has a list of all files
I'm trying to list all the files in a directory and then write that
I'm trying to figure out how to list all files of an FTP folder
I am trying to copy all the files in a list of directories 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.