c++ - Get list of files with same extension and process them -


i'm having trouble implementing top answer here: how list of files specific extension in given folder

i attempting of ".vol" files in directory argv[2] , batch processing each file find. want pass each file parsefile function takes string argument.

// return filenames of files have specified extension // in specified directory , subdirectories vector<string> get_all(const boost::filesystem::path& root, const string& ext, vector<boost::filesystem::path>& ret){     if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return vector<string>();      boost::filesystem::recursive_directory_iterator it(root);     boost::filesystem::recursive_directory_iterator endit;      while(it != endit)     {         if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());         ++it;         cout << *it << endl;          return *ret;   // errors here     } }    ... main function   if (batch) {    vector<boost::filesystem::path> retvec;    vector<boost::filesystem::path> volumevec = get_all(boost::filesystem::path(string(argv[2])), string(".vol"), retvec);   // convert volume files in volumevec strings , pass parsefile    parsefile(volumefilestrings);  } 

i having trouble get_all function , how return vector correctly.

change return statement vector<boost::filesystem::path> , remove ret parameters function , instead create ret in function so:

vector<boost::filesystem::path> ret; 

then you'll want move return statement of ret, return ret;, below while loop appends of file names ret.

your code this:

vector<boost::filesystem::path> get_all(const boost::filesystem::path& root, const string& ext){     if(!boost::filesystem::exists(root) || !boost::filesystem::is_directory(root)) return;      boost::filesystem::recursive_directory_iterator it(root);     boost::filesystem::recursive_directory_iterator endit;     vector<boost::filesystem::path> ret;     while(it != endit)     {         if(boost::filesystem::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());         ++it;         cout << *it << endl;      }     return ret; } 

Comments