Created
June 8, 2009 04:58
-
-
Save martinisoft/125628 to your computer and use it in GitHub Desktop.
Revisions
-
martinisoft renamed this gist
Jun 8, 2009 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
martinisoft revised this gist
Jun 8, 2009 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,43 +7,56 @@ using namespace std; // getdir - returns vector of files in all directories of directory int getdir(string dir, vector<string> &files) { // directory pointer DIR *dp; // dirent struct for directory struct dirent *dirp; // attempt to open directory if((dp = opendir(dir.c_str())) == NULL) { cout << "Error(" << errno << ") opening " << dir << endl; return errno; } // Loop through each file in the directory while ((dirp = readdir(dp)) != NULL) { // Recurse into subdirectory if found if (dirp->d_type == DT_DIR && strcmp(dirp->d_name,".") != 0 && strcmp(dirp->d_name,"..") != 0) { string p = dir + "/" + dirp->d_name; getdir(p, files); } // Found a hidden file, skip it else if (dirp->d_type == DT_REG && string(dirp->d_name).substr(0, 1) == ".") { continue; } // Add file to list else if (dirp->d_type == DT_REG) { files.push_back(string(dirp->d_name)); } } // Close directory pointer closedir(dp); return 0; } int main() { // Directory to list string dir = string("."); // Vector of strings to hold filenames vector<string> files = vector<string>(); // getdir to list the files getdir(dir,files); // Loop through the results for (unsigned int i = 0;i < files.size();i++) { cout << files[i] << endl; -
martinisoft created this gist
Jun 8, 2009 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ #include <sys/types.h> #include <dirent.h> #include <errno.h> #include <vector> #include <string> #include <iostream> using namespace std; int getdir(string dir, vector<string> &files) { DIR *dp; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { cout << "Error(" << errno << ") opening " << dir << endl; return errno; } while ((dirp = readdir(dp)) != NULL) { if (dirp->d_type == DT_DIR && strcmp(dirp->d_name,".") != 0 && strcmp(dirp->d_name,"..") != 0) { string p = dir + "/" + dirp->d_name; getdir(p, files); } else if (dirp->d_type == DT_REG && string(dirp->d_name).substr(0, 1) == ".") { continue; } else if (dirp->d_type == DT_REG) { files.push_back(string(dirp->d_name)); } } closedir(dp); return 0; } int main() { string dir = string("."); vector<string> files = vector<string>(); getdir(dir,files); for (unsigned int i = 0;i < files.size();i++) { cout << files[i] << endl; } return 0; }