C ++ - load the entire file name + count the number of files in the current directory + filter file extension

I want to count the number of files in the current directory, and also download all the file names in the array. If possible, I want to know how to filter the file extension as well

+4
source share
2 answers

Associate the following program with -lboost_filesystem

 #include <iostream> #include <string> #include <vector> #include <boost/algorithm/string/case_conv.hpp> #include <boost/filesystem.hpp> int main( int argc, char ** argv ) { std::string ext = ".jpg"; std::vector<std::string> files; for ( boost::filesystem::directory_iterator it( boost::filesystem::initial_path() ); it != boost::filesystem::directory_iterator(); ++it ) { if ( boost::filesystem::is_regular_file( it->status() ) && boost::algorithm::to_lower_copy( it->path().extension() ) == ext ) { files.push_back( it->path().filename() ); } } std::cout << "Number of files: " << files.size() << std::endl; std::copy( files.begin(), files.end(), std::ostream_iterator<std::string>( std::cout, "\n" ) ); return 0; } 
+8
source

The answer is Boost.Filesystem , in particular directory_iterator .

0
source

Source: https://habr.com/ru/post/1303272/


All Articles