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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:49:39+00:00 2026-05-18T19:49:39+00:00

i’m new in c++ world, i just use it for litle app that help

  • 0

i’m new in c++ world, i just use it for litle app that help me in my work, now, i need to read the content of a folder, list the folder content, i’ve made a function that return a pointer with the name of every obj in the folder, but now, i don’t know how to read the content of the pointer to just print it in a console, my function look like this

string* listdir (const char *path)
{
    string* result = new string[50]; // limit to 50 obj
    DIR *pdir = NULL;
    pdir = opendir (path);
    struct dirent *pent = NULL;
    if (pdir == NULL)
    { 
        printf ("\nERROR! pdir could not be initialised correctly");
        return NULL;
    } 
    int i = 0;
    while (pent = readdir (pdir))
    {
        if (pent == NULL)
        {
            printf ("\nERROR! pent could not be initialised correctly");
            return NULL;
        }
        //printf ("%s\n", pent->d_name);
        result[i++]= pent->d_name;
    }

    closedir (pdir);
    return result;
}

i’ve been trying to print the result of teh function

int main()
{
    string *dirs;
    dirs = listdir("c:\\");
    int i = 0;
    //while(dirs[i])
    //{
            //cout<<dirs[i]<<'\n';
            //++i;
    //}
}

but i really don’t know what i’m doing, lol, some help would be perfect
thanks

  • 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-18T19:49:40+00:00Added an answer on May 18, 2026 at 7:49 pm

    In C++, dereferencing a pointer is achieved using the * operator, just like in ‘C’.

    However, there are a number of problems with your code, which I have addressed here because I was bored…

    #include <string>
    #include <iostream>
    #include <list>
    #include <dirent.h>
    
    typedef std::list<std::string> dir_list;
    
    bool listdir(const std::string& path, dir_list& result)
    {
        bool retval = true;
        DIR* pdir = opendir(path.c_str());
        if (pdir == NULL)
        {
            std::cerr << "ERROR! pdir could not be initialised correctly" << std::endl;;
            retval = false;
        }
        else
        {
            for (dirent* pent = readdir(pdir); pent != NULL; pent = readdir(pdir))
            {
                if (pent == NULL && result.empty())
                {
                    std::cerr << "ERROR! pent could not be initialised correctly" << std::endl;
                    retval = false;
                }
    
                if (result.size() < 50)
                {// *really* limit to 50!
                    result.push_back(pent->d_name);
                }
            }
            closedir(pdir);
        }
        return retval;
    }
    
    int main()
    {
        dir_list dirs;
        if (listdir("C:/", dirs))
        {
            for (dir_list::const_iterator iter(dirs.begin()), end(dirs.end()); iter != end; ++iter)
            {
                std::cout << *iter << std::endl;
            }
        }
    }
    

    Since you’re using C++, STL, its string and container classes will save you a World of pointer pain!

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
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 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
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.