| | |
Recursively Search Through Directories
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 16
Reputation:
Solved Threads: 0
I'm trying to recursively search through directories and pull out any files or subdirectories.
This is the code I have:
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???
This is the code I have:
C++ Syntax (Toggle Plain Text)
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; }
Last edited by Narue; Oct 7th, 2008 at 12:47 pm. Reason: added code tags
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
Usage:
DirTraveler.h
DirTraveler.cpp
c++ Syntax (Toggle Plain Text)
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
c++ Syntax (Toggle Plain Text)
#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
c++ Syntax (Toggle Plain Text)
#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); } } }
![]() |
Similar Threads
- Yes,JMenuItem can point to a HTML that open in IE but that's where prob appears! (Java)
- code for ......anyone??? (C)
Other Threads in the C++ Forum
- Previous Thread: Parity Check Matrix
- Next Thread: problem with calling inheritance class objects using vector
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






