C++, Boost, how to get a path name that does not exist anymore? -


i'm using boost::filesystem iterate through of directories , files under root(let c:\\) path given.

i have used recursive_directory_iterator initialize vector<boost::filesystem::path> paths contains of paths under root.

however of paths have been removed directory , i'm printing message saying "a path no longer exist, path is..". i'm not sure how path name no longer exist(as cannot access vector pathname).

code added requested*

for (size_t = 0; < this->paths.size(); i++) {     if (boost::filesystem::exists(this->paths[i])) {         /* path list of path, ps */         boost::filesystem::path p = this->paths[i];          /* convert path string */         string path = p.string();          /* check file size(should not on 10mb) */         if (get_file_size(path) > 10 * pow(10, 6)) continue;          /* setting extension */         string file_extension = boost::filesystem::extension(path);          /* if current path stands text file */         if (!file_extension.compare(this->extension)) notify_changes(p);     } else {         cout << "a path no longer exist, path is.." << endl;         this->paths.erase(this->paths.begin() + i);     } } 

loops

the old school loop approach:

live on coliru

#include <boost/filesystem.hpp> #include <iostream>  namespace fs = boost::filesystem;  int main() {     if (system("bash -c 'mkdir -pv tree/{a,b,c}/subdir/; touch tree/{a,b,c}/subdir/{a,b,c}.txt'"))         perror("whoops");      std::vector<fs::path> paths { fs::recursive_directory_iterator { "tree/" }, {} };      if (system("bash -c 'rm -rfv tree/b/subdir/ tree/*/subdir/b.txt'"))         perror("whoops");      (auto = paths.begin(); != paths.end();) {         auto& p = *it;         if (!fs::exists(p))         {             std::cout << "no longer exists: " << p << "\n";             = paths.erase(it);             continue;         }          // processing          ++it;     } } 

using algorithms

live on coliru

#include <boost/filesystem.hpp> #include <boost/range/algorithm.hpp> #include <iostream>  namespace fs = boost::filesystem;  bool try_to_process(fs::path const& p) // return false if file wasn't found {     if (!fs::exists(p))     {         std::cout << "no longer exists: " << p << "\n";         return false;     }      // processing      return true; }  int main() {     if (system("bash -c 'mkdir -pv tree/{a,b,c}/subdir/; touch tree/{a,b,c}/subdir/{a,b,c}.txt'"))         perror("whoops");      std::vector<fs::path> paths { fs::recursive_directory_iterator { "tree/" }, {} };      if (system("bash -c 'rm -rfv tree/b/subdir/ tree/*/subdir/b.txt'"))         perror("whoops");      paths.erase(boost::stable_partition(paths, try_to_process), paths.end()); } 

outputs

prints

mkdir: created directory `tree' mkdir: created directory `tree/a' mkdir: created directory `tree/a/subdir/' mkdir: created directory `tree/b' mkdir: created directory `tree/b/subdir/' mkdir: created directory `tree/c' mkdir: created directory `tree/c/subdir/' removed `tree/b/subdir/c.txt' removed `tree/b/subdir/b.txt' removed `tree/b/subdir/a.txt' removed directory: `tree/b/subdir' removed `tree/a/subdir/b.txt' removed `tree/c/subdir/b.txt' no longer exists: "tree/b/subdir" no longer exists: "tree/b/subdir/c.txt" no longer exists: "tree/b/subdir/b.txt" no longer exists: "tree/b/subdir/a.txt" no longer exists: "tree/a/subdir/b.txt" no longer exists: "tree/c/subdir/b.txt" 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -