#include <dirent.h>
#include <stdio.h>
#include <string.h>
void list_files(const char *path) {
DIR *dp = opendir(path);
if (!dp) { perror("opendir"); return; }
struct dirent *ep;
while ((ep = readdir(dp))) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue;
printf("%s\n", ep->d_name);
}
closedir(dp);
}
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void list_files_windows(const char *path) {
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile((std::string(path) + "\\*").c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) return;
do {
if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0) continue;
_tprintf(TEXT("%s\n"), fd.cFileName);
} while (FindNextFile(h, &fd));
FindClose(h);
}
#ifdef _WIN32
// Windows 實現
#else
// POSIX 實現
#endif
或使用 libuv、Boost.Filesystem 等成熟庫屏蔽平台差異。