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
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…
Since you’re using C++, STL, its
stringand container classes will save you a World of pointer pain!