In the realm of programming, file manipulation and directory traversal are essential tasks that every developer encounters at some point. Whether you’re building a file management application, processing data, or simply need to list files in a directory, C++ provides powerful tools and libraries to help you achieve these goals. In this article, we’ll delve into the world of C++ and learn how to list files in a directory with ease.

Prerequisites

Before we dive into the code, it’s essential to have a basic understanding of C++ programming concepts. Additionally, make sure you have a C++ development environment set up on your system. You can use popular IDEs like Visual Studio, Code::Blocks, or even a simple text editor and a C++ compiler like g++.

Getting Started

To list files in a directory using C++, we’ll leverage the Standard Library’s <filesystem> header, introduced in C++17. This header provides a comprehensive set of functions and classes for file and directory operations. Here’s a step-by-step guide on how to get started:

Let’s envision a scenario where we aim to fill a vector with the names of files from a directory using a read_directory() function as follows:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
 
typedef std::vector<std::string> stringvec;
 
int main()
{
    stringvec v;
    read_directory(".", v);
    std::copy(v.begin(), v.end(),
         std::ostream_iterator<std::string>(std::cout, "\n"));
}

There exist four methods at our disposal for this purpose:

  • Use boost::filesystem::directory_iterator;
  • Use std::filesystem::directory_iterator (C++17);
  • Use opendir()/readdir()/closedir() (POSIX);
  • Use FindFirstFile()/FindNextFile()/FindClose() (Windows).
Code snippets on a dark-themed editor highlighting a WordPress function

Method 1: Use boost::filesystem

#include <boost/filesystem.hpp>
 
struct path_leaf_string
{
    std::string operator()(const boost::filesystem::directory_entry& entry) const
    {
        return entry.path().leaf().string();
    }
};
 
void read_directory(const std::string& name, stringvec& v)
{
    boost::filesystem::path p(name);
    boost::filesystem::directory_iterator start(p);
    boost::filesystem::directory_iterator end;
    std::transform(start, end, std::back_inserter(v), path_leaf_string());

Method 2: Use std::filesystem (C++17)

#include <filesystem>
 
struct path_leaf_string
{
    std::string operator()(const std::filesystem::directory_entry& entry) const
    {
        return entry.path().leaf().string();
    }
};
 
void read_directory(const std::string& name, stringvec& v)
{
    std::filesystem::path p(name);
    std::filesystem::directory_iterator start(p);
    std::filesystem::directory_iterator end;
    std::transform(start, end, std::back_inserter(v), path_leaf_string());
}

Method 3: Use opendir()/readdir()/closedir() (POSIX)

#include <sys/types.h>
#include <dirent.h>
 
void read_directory(const std::string& name, stringvec& v)
{
    DIR* dirp = opendir(name.c_str());
    struct dirent * dp;
    while ((dp = readdir(dirp)) != NULL) {
        v.push_back(dp->d_name);
    }
    closedir(dirp);
}

Method 4: Use FindFirstFile()/FindNextFile()/FindClose() (Windows)

#include <windows.h>
 
void read_directory(const std::string& name, stringvec& v)
{
    std::string pattern(name);
    pattern.append("\\*");
    WIN32_FIND_DATA data;
    HANDLE hFind;
    if ((hFind = FindFirstFile(pattern.c_str(), &data)) != INVALID_HANDLE_VALUE) {
        do {
            v.push_back(data.cFileName);
        } while (FindNextFile(hFind, &data) != 0);
        FindClose(hFind);
    }
}

Conclusion

Before diving deep into the C++ methods for directory operations, it’s worth noting the similarities and differences with other languages. For those curious about how languages like Python handle memory and classes,you can read here about Python’s class static variables, offering an interesting parallel in design philosophy. With that background, this article serves as a comprehensive guide on utilizing C++ to efficiently list files in directories. It covers essential prerequisites, including a basic understanding of C++ and the necessary development environment. The article introduces the <filesystem> header in C++17 for simplifying file and directory operations and presents four distinct methods for listing files, catering to various system environments. These methods include Boost’s filesystem, C++17’s std::filesystem, the POSIX-compliant opendir(), readdir(), and closedir() approach, as well as the Windows-specific FindFirstFile(), FindNextFile(), and FindClose() method.

Leave a Reply