thanks bertha.
your code above produced this error when i tried to compile it:
1 2 3
|
dir_cppdotcom-01.cpp: In function ‘void my_func(char*)’:
dir_cppdotcom-01.cpp:51: error: invalid conversion from ‘int (*)(const dirent**, const dirent**)’ to ‘int (*)(const void*, const void*)’
dir_cppdotcom-01.cpp:51: error: initializing argument 4 of ‘int scandir(const char*, dirent***, int (*)(const dirent*), int (*)(const void*, const void*))’
|
while it seems considerably less robost than your code, i have
gotten this to do what i've needed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#include <algorithm> //added from computing.net tip
#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);
sort (files.begin(), files.end()); //added from computing.net tip
return 0;
}
int main()
{
string firstfile = "";
// string dir = string("."); //this directory
string dir = string("/home/babag/Documents/Scripts/receiving");
vector<string> files = vector<string>();
getdir(dir,files);
firstfile = files[2]; //print first file after (.) and (..)
cout << firstfile << endl;
/*this for loop prints directory contents*/
// for (unsigned int i = 2;i < files.size();i++) { //i=0 includes . and ..; i=1 includes ..; i=2 starts on first file
// cout << files[i] << endl; //print each file in directory
// cout << files[0] << endl; //print first file in directory (.) or 'this directory' symbol
// }
return 0;
}
|
i'd be curious for comments.
one thing i notice is that you have a routine for excluding . and ..
in the return. not sure how i'd incorporate that.
also, the code i have has another limitation in that it distinguishes
between upper and lowercase filenames. that, in effect, makes for
two separate sets of returns, one uppercase, the other lower.
does your code put all files into a single sort? or are upper and
lowercase sorted separately?
thanks very much for the help,
BabaG