Why might you need a list of files in C++?
Imagine: you are writing a program that needs to process hundreds of logs, check for the presence of configuration files, or simply show the user what is in a folder. Without the ability to "look" inside a directory, you hit a wall. That is why the question "How can I get the list of files in a directory using C or C++?" is one of the most popular on Stack Overflow. And today we will break it down from start to finish.
In this article, I will show three main methods: from the classic C-style (works everywhere, but requires care) to the modern C++17 std::filesystem (concise, safe, and elegant). You will learn how to choose the right tool for your task and get ready-made code examples that you can immediately insert into your project.
Method 1: The Classic — dirent.h (POSIX / Linux / macOS)
This method is the "grandfather" of all solutions. It is used in C and C++ on Unix-like systems. The idea is simple: open a directory, read entries one by one, close it. No frills, but full control.
Includes and Structures
You will need:
#include <dirent.h>— working with directoriesDIR *dir— pointer to the directory streamstruct dirent *entry— structure containing the file name
Code Example: Simple Output of All Names
#include <iostream>#include <dirent.h>
int main() { const char* path = "."; // current folder DIR* dir = opendir(path); if (dir == nullptr) { std::cerr << "Failed to open directory: " << path << std::endl; return 1; }
struct dirent* entry; while ((entry = readdir(dir)) != nullptr) { std::cout << entry->d_name << std::endl; }
closedir(dir); return 0;}Important Nuances
- The
readdir()function returns the names.(current directory) and..(parent directory). Usually, you need to skip them:
if (entry->d_name[0] == '.') continue;- This code **does not work on Windows** out of the box. For Windows, there is
windows.handFindFirstFile/FindNextFile(more on that later). - Pros: minimalism, works without C++17. Cons: not cross-platform, no filtering by file type.
Method 2: Windows API — FindFirstFile and FindNextFile
If your target is Windows, forget about dirent.h. Instead, use the native WinAPI. It is powerful, but the code is verbose.
How It Works
- Call
FindFirstFilewith a pattern (e.g.,"C:\\\\*"). - Get a handle and the first entry.
- In a loop, call
FindNextFileuntil it returns 0. - Close the handle with
FindClose.
Example: List Files in the Root of Drive C:
#include <iostream>#include <windows.h>
int main() { WIN32_FIND_DATA findFileData; HANDLE hFind = FindFirstFile("C:\\\\*", &findFileData);
if (hFind == INVALID_HANDLE_VALUE) { std::cerr << "Failed to find files (error code: " << GetLastError() << ")" << std::endl; return 1; }
do { // Skip "." and ".." if (strcmp(findFileData.cFileName, ".") != 0 && strcmp(findFileData.cFileName, "..") != 0) { std::cout << findFileData.cFileName << std::endl; } } while (FindNextFile(hFind, &findFileData) != 0);
FindClose(hFind); return 0;}What You Need to Know
- The
WIN32_FIND_DATAstructure has adwFileAttributesfield