The Artima Developer Community
Sponsored Link

The C++ Source
Listing 2 - Navigates directory entries using unixstl::readdir_sequence

Shows how to use the readdir_seauence class to traverse directory entries. Referenced in Reading Unix-style Directories via STL-compliant Sequences.

#include <unixstl_readdir_sequence.h> // unixstl::readdir_sequence

#include <algorithm>                  // std::copy
#include <iterator>                   // std::ostream_iterator
#include <iostream>                   // std::cout, std::endl
#include <string>                     // std::string
#include <vector>                     // std::vector

using std::copy;
using std::cout;
using std::endl;
using std::ostream_iterator;
using std::string;
using std::vector;
using unixstl::readdir_sequence;

const char HOME[] = "/home/matty/";

int main()
{
  readdir_sequence dir(HOME, readdir_sequence::directories);
  vector<string>   dirNames;

  for(readdir_sequence::const_iterator b = dir.begin(); b != dir.end(); ++b)
  {
    dirNames.push_back((*b)->d_name);
  }

  cout << "Dumping subdirectories of " << HOME << endl;
  copy(dirNames.begin(), dirNames.end(), ostream_iterator<string>(cout, "\n"));

  return 0;
}



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us