List Files Within a Directory in C

To list files within a directory using C, you can use built-in functions provided by your operating system.

For Unix-like systems, the POSIX functions opendir(), readdir(), and closedir() are commonly used.

On Windows, similar functionality is available through the Windows API functions FindFirstFile(), FindNextFile(), and FindClose().


Example 1: Listing Files Using POSIX Directory Functions

In this example, we will use the POSIX directory functions to open a directory, read its contents, and print the names of the files and subdirectories within the current directory.

main.c

</>
Copy
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>

int main() {
    DIR *dir;
    struct dirent *entry;
    char *path = ".";  // Current directory

    // Open the directory
    dir = opendir(path);
    if (dir == NULL) {
        perror("Unable to open directory");
        return EXIT_FAILURE;
    }

    // Read and print each entry in the directory
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\n", entry->d_name);
    }

    // Close the directory
    closedir(dir);
    return EXIT_SUCCESS;
}

Explanation:

  1. path is set to “.” which represents the current directory.
  2. opendir(path) opens the directory specified by path and returns a pointer to a DIR structure. If it fails, NULL is returned.
  3. The while loop uses readdir() to read each directory entry, storing the result in the entry pointer of type struct dirent*.
  4. printf() prints the name of each file or subdirectory using entry->d_name.
  5. closedir() closes the directory stream to free up resources.

Output:

file1.txt
file2.c
directory1
...

Example 2: Listing Files Using Windows API Functions

In this example, we will use the Windows API functions to list files in the current directory. The program employs FindFirstFile() to initiate the search and FindNextFile() in a loop to retrieve subsequent files.

main.c

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

int main() {
    WIN32_FIND_DATA findFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    char *directory = ".\\*";  // Search pattern for current directory

    // Start the file search
    hFind = FindFirstFile(directory, &findFileData);
    if (hFind == INVALID_HANDLE_VALUE) {
        printf("Unable to open directory. Error: %d\n", GetLastError());
        return 1;
    } 

    // Loop through the directory entries
    do {
        printf("%s\n", findFileData.cFileName);
    } while (FindNextFile(hFind, &findFileData) != 0);

    // Close the search handle
    FindClose(hFind);
    return 0;
}

Explanation:

  1. directory contains the search pattern “.\*” which tells the program to list all files in the current directory.
  2. FindFirstFile() initiates the search and returns a handle, while also filling the WIN32_FIND_DATA structure with details about the first file found.
  3. The do-while loop continues calling FindNextFile() to retrieve subsequent files until no more entries are found.
  4. printf() prints each file name stored in findFileData.cFileName.
  5. FindClose() closes the search handle once the search is complete.

Output:

file1.txt
file2.cpp
directory1
...

Conclusion

In this tutorial, we explored two methods to list files within a directory using C:

  1. POSIX Approach: Uses opendir(), readdir(), and closedir() to traverse directories on Unix-like systems.
  2. Windows API Approach: Uses FindFirstFile(), FindNextFile(), and FindClose() to list directory contents on Windows systems.