Hello there. i need to do this thingy... i have to write a code with which i could enter a path to directory and it would show every folder and file in that directory on the screen. also i need to add theese names into a massive... i got this code, but this is a bit too hard for me, is it possible to make it simple? if not, then could you explain few things in it? please..
for example this 'std::string const & path
, std::vector <std::string> & dirEntryNames' ??
the code i have for list of files in directory:
Code:
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
int dirfunct
( std::string const & path
, std::vector <std::string> & dirEntryNames )
{
DIR * dir = opendir( path.c_str() );
if ( dir != NULL )
{
dirent * entry( NULL );
do
{
entry = readdir( dir );
if ( entry != NULL )
{
dirEntryNames.push_back( entry->d_name );
}
}
while ( entry!= NULL );
if ( closedir( dir ) )
{
return -1;
}
}
else
{
return -1;
}
return 0;
}
int main()
{
std::string path;
std::cout << "Path: ";
std::cin >> path;
std::vector<std::string> entries;
if ( !dirfunct(path, entries) )
{
std::cout << "Contents of path:\n";
for ( int i=0; i < entries.size(); ++i )
{
std::cout << entries[i] << "\n";
}
}
}