Skip to content

Instantly share code, notes, and snippets.

@psychoss
Created January 16, 2016 01:14
Show Gist options
  • Save psychoss/ef24c989b0c717452c8e to your computer and use it in GitHub Desktop.
Save psychoss/ef24c989b0c717452c8e to your computer and use it in GitHub Desktop.
C++
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
/*function... might want it in some class?*/
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) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment