List of files given a Path in C++ -
i trying create program randomly chooses folder given base path, within new folder randomly chooses video opened , start playing.
my main issue finding number of files within path given. there function can that? or similar? kind of headers need? etc..
the random part kind of easy. after fixing issue know if able launch video example while executing program, should last step of program.
i have searched lot before posting that, know might think out there, not able find specific enough want.
i hope can me.
you should boost.filesystem. c++ without boost (or library set, qt) has limited capabilities.
there an example in doc:
int main(int argc, char* argv[]) { path p (argv[1]); // p reads clearer argv[1] in following code try { if (exists(p)) // p exist? { if (is_regular_file(p)) // p regular file? cout << p << " size " << file_size(p) << '\n'; else if (is_directory(p)) // p directory? { cout << p << " directory containing:\n"; copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type ostream_iterator<directory_entry>(cout, "\n")); // directory_entry, // converted path // path stream inserter } else cout << p << " exists, neither regular file nor directory\n"; } else cout << p << " not exist\n"; } catch (const filesystem_error& ex) { cout << ex.what() << '\n'; } return 0; }
of course can use directory_iterator
inside "for" loop:
#include <boost/filesystem.hpp> #include <boost/range/iterator_range.hpp> #include <iostream> using namespace boost::filesystem; int main(int argc, char *argv[]) { path p(argc > 1? argv[1] : "."); if(is_directory(p)) { std::cout << p << " directory containing:\n"; for(auto& entry : boost::make_iterator_range(directory_iterator(p), {})) std::cout << entry << "\n"; } }
Comments
Post a Comment