How to get all files in a given directory using C++ on windows?
Note:
I found methods that use dirent.h
but I need a more standard way...
Thanks
How to get all files in a given directory using C++ on windows?
Note:
I found methods that use dirent.h
but I need a more standard way...
Thanks
You have to use the FindFirstFile
function (documented here). This is the standard (and preferred) way in Windows, however it is not portable. The header dirent.h
you have found contains the definition of the standard POSIX functions.
For the full code look at this example: Listing the Files in a Directory
Use FindFirstFile and related functions. Example:
HANDLE hFind;
WIN32_FIND_DATA data;
hFind = FindFirstFile("c:\\*.*", &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
printf("%s\n", data.cFileName);
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
The accepted standard for C++ is described in N1975. Your compiler might not have it yet, in which case Boost.FileSystem provides essentially the same.