Reading the Contents of a Directory - C++ File Stream

C++ examples for File Stream:Directory

Description

Reading the Contents of a Directory

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

using namespace boost::filesystem;

int main(int argc, char** argv) {
   path fullPath = system_complete(path("newFolder", native));

   if (!exists(fullPath)) {
      std::cerr << "Error: the directory " << fullPath.string() << " does not exist.\n";
      return(EXIT_FAILURE);
   }

   if (!is_directory(fullPath)) {
      std::cout << fullPath.string() << " is not a directory!\n";
      return(EXIT_SUCCESS);
   }

   directory_iterator end;
   for (directory_iterator it(fullPath);
        it != end; ++it) {               
      std::cout << it->leaf();           
      if (is_directory(*it))             
         std::cout << " (dir)";
      std::cout << '\n';
   }

   return(EXIT_SUCCESS);
}

Related Tutorials