charis
charis

Reputation: 439

Non deterministic execution of boost filesystem directory_iterator

The following code recursively iterates over a given directory and prints its contents in the same order every time.

Is it possible to alter the directory iterator to print the directory contents in a random way (without i.e. using a vector to store the results and then print the vector contents randomly) ?

#include <string>

#include <iostream>
#include <boost/filesystem.hpp>

using namespace std;

int main(int argc, char** argv)
{
  boost::filesystem::path dataPath("/home/test/");
  boost::filesystem::recursive_directory_iterator endIterator;

  // Traverse the filesystem and retrieve the name of each root object found
  if (boost::filesystem::exists(dataPath) && boost::filesystem::is_directory(dataPath)) {
    for (static boost::filesystem::recursive_directory_iterator directoryIterator(dataPath); directoryIterator != endIterator;
     ++directoryIterator) {
      if (boost::filesystem::is_regular_file(directoryIterator->status())) {

        std::string str = directoryIterator->path().string();
        cout << str << endl;
        }
    }
}

}

Upvotes: 0

Views: 226

Answers (1)

Marco A.
Marco A.

Reputation: 43662

Most OSes (e.g. Windows' FindFirstFile) don't return entries in any particular order so there's no way to have them ordered as you want. Your best bet is to do the ordering/shuffling yourself.

Upvotes: 1

Related Questions