Hi all, I have a function that returns the contents of a directory in a string vector. It works perfectly but what I want to do is filter out the results for a certain file type. I just wanted to know if there was anything already available to do this? Or would I just have to take the last 4 characters of the string and compare it? Below is the function I am using to get the contents of a directory.
Code:int getdir (std::string dir, std::vector<std::string> &files) { DIR *dp; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { std::cout << "Error(" << errno << ") opening " << dir << std::endl; return errno; } while ((dirp = readdir(dp)) != NULL) { files.push_back(std::string(dirp->d_name)); } closedir(dp); return 0; }
EDIT:
I have come up with this... but its not perfect… but it works…
so again, is there a better way to do this?
Code:int getdir (std::string dir, std::vector<std::string> &files, std::string filetype) { DIR *dp; struct dirent *dirp; if((dp = opendir(dir.c_str())) == NULL) { std::cout << "Error(" << errno << ") opening " << dir << std::endl; return errno; } while ((dirp = readdir(dp)) != NULL) { if (std::string(dirp->d_name).find(filetype)!=std::string::npos) files.push_back(std::string(dirp->d_name)); } closedir(dp); return 0; }



LinkBack URL
About LinkBacks



