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

To list files in a directory using C or C++, the approach depends on the language and platform. Below are detailed methods with examples for both languages.

1. In C (POSIX Systems)

Use the dirent.h library (POSIX-compliant, works on Linux/macOS and Windows with MinGW or WSL).

Example Code:

#include <stdio.h>
#include <dirent.h>

int main() {
    DIR *dir;
    struct dirent *entry;

    // Open the directory
    dir = opendir(".");
    if (dir == NULL) {
        perror("Failed to open directory");
        return 1;
    }

    // Read directory entries
    while ((entry = readdir(dir)) != NULL) {
        // Skip "." and ".." entries
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
            continue;

        printf("%s\n", entry->d_name);
    }

    closedir(dir);
    return 0;
}

Key Functions:

  • opendir(): Opens a directory stream.
  • readdir(): Reads the next directory entry.
  • closedir(): Closes the directory stream.

Notes:

  • Windows Compatibility: Use dirent.h for Windows to enable POSIX directory operations.
  • Filtering Files: Use stat() to check file types (e.g., regular files vs. directories).

2. In C++ (C++17 and Later)

Use the <filesystem> library (cross-platform, requires C++17 or newer).

Example Code:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
    std::string path = "."; // Current directory

    try {
        for (const auto& entry : fs::directory_iterator(path)) {
            // Skip directories if needed
            if (entry.is_directory()) continue;

            std::cout << entry.path().filename().string() << std::endl;
        }
    } catch (const fs::filesystem_error& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

Key Features:

  • Iterate with directory_iterator: Lists all entries in a directory.
  • Check File Type: Use entry.is_regular_file() or entry.is_directory().
  • Cross-Platform: Works on Windows, Linux, and macOS.

Compilation:

Enable C++17 support (e.g., g++ -std=c++17 -lstdc++fs main.cpp).

3. Platform-Specific Methods

Windows (C/C++ with WinAPI):

Use the Windows API for native file listing:

#include <windows.h>
#include <stdio.h>

int main() {
    WIN32_FIND_DATA findData;
    HANDLE hFind = FindFirstFile("*", &findData);

    if (hFind == INVALID_HANDLE_VALUE) {
        printf("Error opening directory\n");
        return 1;
    }

    do {
        // Skip "." and ".."
        if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
            continue;

        printf("%s\n", findData.cFileName);
    } while (FindNextFile(hFind, &findData));

    FindClose(hFind);
    return 0;
}

Key Differences

MethodLanguagePlatformComplexity
dirent.h (POSIX)CLinux/macOS/WSLLow
<filesystem> (C++17)C++Cross-platformLow
Windows APIC/C++WindowsModerate

Handling Edge Cases

  1. Non-Existent Directory:
  • Check if opendir() returns NULL (C) or catch filesystem_error (C++).
  1. Permissions:
  • Handle errors when accessing restricted directories.
  1. Hidden Files:
  • Filter using filename prefixes (e.g., . on Unix, hidden attributes on Windows).

Summary

  • C: Use dirent.h for POSIX systems or a compatibility layer on Windows.
  • C++: Prefer <filesystem> for cross-platform simplicity.
  • Windows API: Use FindFirstFile/FindNextFile for native Windows code.

Leave a Reply

Your email address will not be published. Required fields are marked *