I'm trying to recursively search through directories and pull out any files or subdirectories.

This is the code I have:

int readDir(string directory, vector<string> &fileList) 
//directory is the name of a direcotry 
//fileList is an empty vector

DIR *dir;
struct dirent *ent;

if((dir = opendir(director.c_str()) == NULL){
     cout<<"Invalid Directory"<<endl;
     exit(1);
}
while((ent = readdir(dir)) != NULL){
     fileList.push_back(string(ent->d_name));
}
closedir(dir);
return 0;
}

As of right now, it can read in a directory and list the files and subdirectories. I want it to now go through each subdirectory and get the files inside. Does anyone know how to go about making this a recursive function???

How about you use code tags and post code that can be compiled.

There are easier ways to get file listings by using the OS list functions & _popen(). The output can be read using fread(). In windows: "dir /s/b ", in Linux: you could try "ls -R -l ".

commented: What can he learn by using "dir /s/b" -3

When you read a file that's a directory, call the function again. And learn to use
CODE tags. There are at least 6 places on the site they are explained.

Usage:

DirTraveler traveler;
vector<string>foo;
traveler.travelDirectoryRecursive("bar", &foo);
for (int i=0; i<foo.size(); ++i)
    cout << foo[i].c_str() << endl;

DirTraveler.h

#ifndef DIRTRAVELER_H
#define DIRTRAVELER_H

#include <string>
#include <vector>

using namespace std;

/// Directory traveler (mostly used with zgui manager)

class DirTraveler
{
    public:
        DirTraveler();
        virtual ~DirTraveler();

        vector<string> travelDirectory(string directory);
        void travelDirectoryRecursive(string directory, vector<string> *fullList);
    protected:
    private:
};

#endif // DIRTRAVELER_H

DirTraveler.cpp

#include "DirTraveler.h"
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <sys/types.h>
#include <dirent.h>
#include <vector>

using namespace std;

DirTraveler::DirTraveler()
{
    //ctor
}

DirTraveler::~DirTraveler()
{
    //dtor
}

vector<string> DirTraveler::travelDirectory(string directory)
{
    // travel thru a directory gathering all the file and directory naems
    vector<string> fileList;
    DIR *dir;
    struct dirent *ent;

    // open a directory
    if ((dir=opendir(directory.c_str())) != NULL)
    {
        while((ent=readdir(dir)) != NULL) // loop until the directory is traveled thru
        {
            // push directory or filename to the list
            fileList.push_back(ent->d_name);
        }
        // close up
        closedir(dir);
    }
    //return the filelust
    return fileList;
}

void DirTraveler::travelDirectoryRecursive(string directory, vector<string> *fullList)
{
    // get the "root" directory's directories
    vector<string> fileList = travelDirectory(directory);

    // loop thru the list
    for (vector<string>::iterator i=fileList.begin(); i!=fileList.end(); ++i)
    {
        // test for . and .. directories (this and back)
        if (strcmp((*i).c_str(), ".") &&
            strcmp((*i).c_str(), ".."))
        {
            // i use stringstream here, not string = foo; string.append(bar);
            stringstream fullname;
            fullname << directory << "/" << (*i);

            fullList->push_back(fullname.str());

            travelDirectoryRecursive(fullname.str(), fullList);
        }
    }
}
commented: why did you post this? -7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.