2

I simply need to put all the files (or file names?) within a directory into a vector so that I can sort them and open them in order.

I literally have no idea how to do this since this my first encounter with C++. I have tried boost::filesystem but I am having no success as this is quite unfamiliar to me. Please help!

I've actually found a method I think will work however I am now struggling with adding a linker to the command line but I think if I ask about that here it'll exceed the scope of the original question.

Ree
  • 863
  • 2
  • 18
  • 38
  • 1
    Try to look at this http://stackoverflow.com/questions/4283546/cboostfilesystem-how-can-i-get-a-list-of-files-in-a-folder-in-which-the-fi – HAL9000 Dec 03 '13 at 08:37

2 Answers2

6
namespace fs = boost::filesystem;
fs::directory_iterator b("path of directory"), e;
std::vector<fs::path> paths(b, e);

This includes sub-directories (but not their contents). If you want to exclude sub-directories, you can use fs::is_directory on a path to check if it is a directory. If you want to include the contents of the sub-directories, then you can use a recursive_directory_iterator instead of a directory_iterator.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
4

taken from your previous question:

std::vector<std::string> filenames;
dpdf = opendir("/data/files");
if (dpdf != NULL) {
   while (epdf = readdir(dpdf)) {
      filenames.push_back(std::string(epdf->d_name));
   }
}
Community
  • 1
  • 1
stefaanv
  • 14,072
  • 2
  • 31
  • 53