1

I'm using the code below, adapted from this thread. I'm able to get the list of files in a folder, but I get segmentation fault at the end. Any idea why this is happening?
And is there a way to get std::string of the current file (complete path) in the for loop?

 boost::filesystem::path path("my_path");

 static void myfunction()
 {
     boost::filesystem::directory_iterator end_itr;
     // path is a static variable, defined somewhere else
     for(boost::filesystem::directory_iterator dir_iter(path); 
                                    dir_iter != end_itr;  ++dir_iter)
         if( boost::filesystem::is_regular_file(dir_iter->status()) )
         {
             // print


         }
  }
Community
  • 1
  • 1
Bob
  • 10,741
  • 27
  • 89
  • 143

1 Answers1

0

perhaps you miss these lines:

namespace fs = boost::filesystem;
fs::directory_iterator end_itr;

and try

cout << "entry: " << dir_iter->path().filename() << "\n";
cout << "entry path: " << dir_iter->path() << "\n";

in the loop

Refs: - http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-directory_entry - http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#class-path

RobbySherwood
  • 361
  • 1
  • 8
  • 1
    I have the end_itr declaration. it is just giving me segmentation fault at the end of the loop – Bob Mar 03 '14 at 21:52