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
#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:
path
is set to “.” which represents the current directory.opendir(path)
opens the directory specified bypath
and returns a pointer to aDIR
structure. If it fails,NULL
is returned.- The
while
loop usesreaddir()
to read each directory entry, storing the result in theentry
pointer of typestruct dirent*
. printf()
prints the name of each file or subdirectory usingentry->d_name
.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
#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:
directory
contains the search pattern “.\*” which tells the program to list all files in the current directory.FindFirstFile()
initiates the search and returns a handle, while also filling theWIN32_FIND_DATA
structure with details about the first file found.- The
do-while
loop continues callingFindNextFile()
to retrieve subsequent files until no more entries are found. printf()
prints each file name stored infindFileData.cFileName
.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:
- POSIX Approach: Uses
opendir()
,readdir()
, andclosedir()
to traverse directories on Unix-like systems. - Windows API Approach: Uses
FindFirstFile()
,FindNextFile()
, andFindClose()
to list directory contents on Windows systems.