Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Iterate over files in the directory

Using opendir, readdir, closedir from STL

Code Block

#include <iostream>
#include <dirent.h>
using namespace std;

int main() {
        DIR*     dir;
        dirent*  pdir;

        dir = opendir(".");      // open current directory
        while (pdir = readdir(dir)) {
                cout << pdir->d_name << endl;
        }
        closedir(dir);
        return 0;
}

Using boost

Code Block
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;

  fs::path path = "/reg/d/psdm/CXI/cxi49012/xtc/e158-r0150-s00-c01.xtc";
  fs::path dir  = path.parent_path(); // i.e.: /reg/d/psdm/CXI/cxi49012/xtc

  typedef fs::directory_iterator dir_iter;
  for (dir_iter dit = dir_iter(dir); dit != dir_iter(); ++ dit) {
    std::cout << dit->path().stem().string() << "\n";  
  } 

...