C programming
C++ programming
File Management
Directory listing
Programming tutorials

How can I get the list of files in a directory using C or C++?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Accessing a list of files in a directory is a common task in many programming situations. Different programming languages offer various methods for accomplishing this, each with its own set of libraries or APIs. Here, we'll focus on how to get the list of files in a directory using C and C++.

Overview of Directory Listing in C and C++

In C and C++, we typically rely on the POSIX library for directory operations in Unix-like systems (Linux, macOS), and the Windows API for Windows systems. The methods differ significantly due to the different system architectures.

Implementing Directory Listing in C on POSIX Systems

For POSIX-compliant systems, which include most Unix-like systems, the dirent.h library is commonly used. This library provides a portable way to interact with the filesystem.

Example in C (POSIX):

Here's a simple example that demonstrates how to list files in a directory using C on POSIX systems:

c
1#include <stdio.h>
2#include <dirent.h>
3
4int main() {
5    DIR *d;
6    struct dirent *dir;
7    d = opendir("."); // Open the current directory
8    if (d) {
9        while ((dir = readdir(d)) != NULL) {
10            printf("%s\n", dir->d_name);
11        }
12        closedir(d);
13    }
14    return 0;
15}

In this code:

  • DIR is a type representing a directory stream.
  • dirent structure represents a directory entry.
  • opendir() opens a directory stream corresponding to the directory name, while readdir() reads a directory entry at a time from the directory stream.
  • closedir() closes the directory stream.

Implementing Directory Listing in C++ on POSIX Systems

In C++, you can use the same dirent.h library as in C, but it is often more common to use higher-level abstractions. However, for standardization and modern practices, filesystem library in C++17 provides a convenient and powerful API for file system operations.

Example in C++17:

cpp
1#include <iostream>
2#include <filesystem>
3namespace fs = std::filesystem;
4
5int main() {
6    std::string path = "."; // Current directory
7    for (const auto & entry : fs::directory_iterator(path))
8        std::cout << entry.path() << std::endl;
9    return 0;
10}

Implementing Directory Listing in C/C++ on Windows Systems

On Windows, the process is different and involves using the Windows API. Here’s a simple example using windows.h.

Example in C (Windows):

c
1#include <windows.h>
2#include <stdio.h>
3
4int main(void) {
5    WIN32_FIND_DATA findFileData;
6    HANDLE hFind = FindFirstFile(".\\*.*", &findFileData);
7
8    if (hFind == INVALID_HANDLE_VALUE) {
9        printf("FindFirstFile failed (%d)\n", GetLastError());
10        return 0;
11    } 
12    do {
13        printf("%s\n", findFileData.cFileName);
14    } while (FindNextFile(hFind, &findFileData) != 0);
15
16    FindClose(hFind);
17    return 0;
18}

This example uses the FindFirstFile and FindNextFile functions to iterate over all files and directories in the current directory.

Comparison Table

FeaturePOSIX (C/C++)Windows (C)
Header Filedirent.hwindows.h
Directory Opening Functionopendir()FindFirstFile()
Directory Reading Functionreaddir()FindNextFile()
Directory Closing Functionclosedir()FindClose()
File/Dir Name Accessdir->d_namefindFileData.cFileName

Conclusion

Listing files in a directory can differ vastly between operating systems and their respective APIs as shown in the examples for POSIX systems and Windows. For modern C++ applications, it’s advisable to use the C++17 filesystem library when possible for portability and ease of use across different platforms.


Course illustration
Course illustration

All Rights Reserved.