C语言遍历文件夹下所有文件
在C语言中遍历文件夹下的所有文件,可以使用dirent.h头文件提供的函数(在Unix/Linux系统上)或Windows.h头文件(在Windows系统上),下面分别介绍这两种方法。

(图片来源网络,侵删)
Unix/Linux系统方法
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
void list_files(const char *path) {
DIR *dir;
struct dirent *entry;
// 打开目录
dir = opendir(path);
if (dir == NULL) {
perror("无法打开目录");
return;
}
// 读取目录中的每个条目
while ((entry = readdir(dir)) != NULL) {
// 跳过"."和".."
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
// 打印文件名
printf("%s\n", entry->d_name);
// 如果是子目录,递归遍历
if (entry->d_type == DT_DIR) {
char new_path[1024];
snprintf(new_path, sizeof(new_path), "%s/%s", path, entry->d_name);
list_files(new_path);
}
}
// 关闭目录
closedir(dir);
}
int main() {
const char *path = "."; // 当前目录
list_files(path);
return 0;
}
Windows系统方法
#include <stdio.h>
#include <windows.h>
void list_files(const char *path) {
WIN32_FIND_DATA find_data;
HANDLE h_find;
char search_path[MAX_PATH];
// 构建搜索路径,"C:\\myfolder\\*"
snprintf(search_path, sizeof(search_path), "%s\\*", path);
// 查找第一个文件
h_find = FindFirstFile(search_path, &find_data);
if (h_find == INVALID_HANDLE_VALUE) {
perror("无法查找文件");
return;
}
do {
// 跳过"."和".."
if (strcmp(find_data.cFileName, ".") == 0 || strcmp(find_data.cFileName, "..") == 0) {
continue;
}
// 打印文件名
printf("%s\n", find_data.cFileName);
// 如果是目录,递归遍历
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
char new_path[MAX_PATH];
snprintf(new_path, sizeof(new_path), "%s\\%s", path, find_data.cFileName);
list_files(new_path);
}
} while (FindNextFile(h_find, &find_data) != 0);
FindClose(h_find);
}
int main() {
const char *path = "."; // 当前目录
list_files(path);
return 0;
}
跨平台方法
如果需要编写跨平台的代码,可以使用预处理器指令来选择适当的实现:
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <sys/types.h>
#endif
void list_files(const char *path) {
#ifdef _WIN32
WIN32_FIND_DATA find_data;
HANDLE h_find;
char search_path[MAX_PATH];
snprintf(search_path, sizeof(search_path), "%s\\*", path);
h_find = FindFirstFile(search_path, &find_data);
if (h_find == INVALID_HANDLE_VALUE) {
perror("无法查找文件");
return;
}
do {
if (strcmp(find_data.cFileName, ".") == 0 || strcmp(find_data.cFileName, "..") == 0) {
continue;
}
printf("%s\n", find_data.cFileName);
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
char new_path[MAX_PATH];
snprintf(new_path, sizeof(new_path), "%s\\%s", path, find_data.cFileName);
list_files(new_path);
}
} while (FindNextFile(h_find, &find_data) != 0);
FindClose(h_find);
#else
DIR *dir;
struct dirent *entry;
dir = opendir(path);
if (dir == NULL) {
perror("无法打开目录");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
printf("%s\n", entry->d_name);
if (entry->d_type == DT_DIR) {
char new_path[1024];
snprintf(new_path, sizeof(new_path), "%s/%s", path, entry->d_name);
list_files(new_path);
}
}
closedir(dir);
#endif
}
int main() {
const char *path = ".";
list_files(path);
return 0;
}
注意事项
- 递归遍历深层目录可能会导致栈溢出,对于非常深的目录结构,可以考虑使用迭代方法(如使用栈或队列)。
- 在Windows系统中,文件路径使用反斜杠(
\),而在Unix/Linux系统中使用正斜杠()。 - 确保程序有足够的权限访问目标目录。
- 对于大型目录,可能需要添加错误处理和超时机制。
代码提供了基本的目录遍历功能,你可以根据需要扩展它,例如获取文件属性、处理特定类型的文件等。

(图片来源网络,侵删)
