Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost directory_iterator example - how to list directory files not recursive

How should I use directory_iterator to list directory files (not recursive)?

Also what header files / libs should I add/link or other settings I should make? I'm using boost in my project but by some reason directory_iterator is "underclared identifier" while I can use other boost features.

Update

Another solution:

#include <filesystem> #include <boost/filesystem.hpp> #include <iostream>  using namespace boost::filesystem;  for (directory_iterator itr(path_ss); itr!=directory_iterator(); ++itr) {     cout << itr->path().filename() << ' '; // display filename only     if (is_regular_file(itr->status())) cout << " [" << file_size(itr->path()) << ']';     cout << '\n'; } 
like image 355
Oleg Vazhnev Avatar asked Jan 04 '14 16:01

Oleg Vazhnev


People also ask

How do I get a list of files in a directory in C++?

Use opendir/readdir Functions to Get a List of Files in a Directory. This method is much more verbose, but it's a reliable alternative for the file system manipulation. To use the functions opendir / readdir , you should include the dirent. h header file.

What is boost directory?

The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.


2 Answers

The tut3 example is what you're looking for:

See it Live on Coliru

Here's a simplified version based on c++11:

Live On Coliru

#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 << " is a directory containing:\n";          for(auto& entry : boost::make_iterator_range(directory_iterator(p), {}))             std::cout << entry << "\n";     } } 

You can see I linked boost_system (for the errorcode facilities) and boost_filesystem:

g++ -std=c++11 -Os -Wall -pedantic main.cpp -lboost_system -lboost_filesystem && ./a.out . "." is a directory containing: "./main.cpp" "./a.out" 
like image 108
sehe Avatar answered Sep 23 '22 01:09

sehe


A tested solution:

#include <boost/filesystem.hpp> #include <iterator> #include <iostream> #include <vector>  using namespace boost::filesystem;  int main(int argc, char *argv[]) {     path p(argc>1? argv[1] : ".");     std::vector<directory_entry> v; // To save the file names in a vector.      if(is_directory(p))     {         copy(directory_iterator(p), directory_iterator(), back_inserter(v));         std::cout << p << " is a directory containing:\n";          for ( std::vector<directory_entry>::const_iterator it = v.begin(); it != v.end();  ++ it )         {             std::cout<< (*it).path().string()<<endl;         }         } } 
like image 26
Shaobo Zi Avatar answered Sep 21 '22 01:09

Shaobo Zi