Skip to content

Instantly share code, notes, and snippets.

@martinisoft
Created June 8, 2009 04:58
Show Gist options
  • Save martinisoft/125628 to your computer and use it in GitHub Desktop.
Save martinisoft/125628 to your computer and use it in GitHub Desktop.

Revisions

  1. martinisoft renamed this gist Jun 8, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. martinisoft revised this gist Jun 8, 2009. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions gistfile1.hpp
    Original 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;
  3. martinisoft created this gist Jun 8, 2009.
    52 changes: 52 additions & 0 deletions gistfile1.hpp
    Original 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;
    }