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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:13:41+00:00 2026-05-25T12:13:41+00:00

I’m new to C++, I have experience with C#, Objective-C and JavaScript. At the

  • 0

I’m new to C++, I have experience with C#, Objective-C and JavaScript.

At the moment I’m trying to write a function that takes a path and returns a directory listing (all files and folders at that path). I’m doing this on Ubuntu.

Here’s my code so far, to be honest I’m struggling to understand the double pointer syntax and what it’s achieving but this is where my googling has lead me…

int FileManager::GetDirectoryListing(char *path, dirent **directoryEntries)
{
    // Debug output...
    printf("Listing directory at %s\n", path);

    // Allocate memory for the directory entries
    *directoryEntries = new dirent[MAX_FILES];

    // Open the path we were provided
    DIR *directory = opendir(path);

    // A counter of how many entries we have read
    int entryCount = 0;

    // Make sure we were able to open the directory
    if(directory) {

        printf("Successfully opened directory\n");

        // Read the first entry in the directory
        struct dirent *directoryEntry = readdir(directory);

        // While we have a directory entry
        while(directoryEntry) {

            // Debug output...
            printf("%s\n", directoryEntry->d_name);

            // Copy the directory entry to the array of directory entries we will return
            memcpy(&directoryEntries[entryCount], directoryEntry, sizeof(struct dirent));

            // Increase our counter
            ++entryCount;

            // Read the next directory
            directoryEntry = readdir(directory);
        }

        // Close the directory
        closedir(directory);
    }

    return entryCount;
}

And then I call this function by:

    dirent *directoryEntries = NULL;

    int numberOfEntries = FileManager::GetDirectoryListing(deviceRootPath, &directoryEntries);

    printf("File Manager returned directory listing.\n");

    for(int i = 0; i < numberOfEntries; ++i) {

        printf("Looping through directory entries, at index: %i\n", i);

        printf("%s\n", directoryEntries[i].d_name);
    }

It’s locking up when it tries to access the first element in directoryEntries i.e. The first time around the loop.

I know I’m not understanding what the double pointer is doing and I don’t have a clear picture in my head about the structure of directoryEntries after the call to GetDirectoryListing.

What is happening and what is the correct way to loop through directoryEntries?

  • 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-25T12:13:41+00:00Added an answer on May 25, 2026 at 12:13 pm
    *directoryEntries = new dirent[MAX_FILES];
    

    What if the number of directories is greater than MAX_FILES? How do you know that it cannot be greater than MAX_FILES?

    I think you should use std::vector<dirent> instead of dirent*. Many of the problems will be solved.

    I would implement the function as:

    std::vector<dirent> FileManager::GetDirectoryListing(char *path)
    {
        std::vector<dirent> dirs;
        DIR *directory = opendir(path);
        if(directory) {
            struct dirent *directoryEntry = readdir(directory);
            while(directoryEntry) {
                dirs.push_back(*directoryEntry); //push a copy of the original!
                directoryEntry = readdir(directory);
            }
            closedir(directory);
        }
        return dirs;
    }
    

    Modern compilers will most probably optimize this code, to avoiding copy of the return value. This optimization is called:

    • Return Value Optimization (RVO) (or Named RVO)

    Also note that directories.size() will tell you the number of entries. So at call site, you can simply do this:

    std::vector<dirent> dirs = FileManager::GetDirectoryListing(deviceRootPath);
    for(size_t i = 0; i < dirs.size() ; ++i)
    {
      std::cout << dirs[i].d_name << std:endl;
    }
    

    In general, prefer std::cout over printf, as the latter is not safe!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.