Files in directory in C++

30,084

Solution 1

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);
}

Solution 2

What about the boost library: filesystem. Boost.org

Solution 3

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

Solution 4

The accepted standard for C++ is described in N1975 ISO/IEC TS 18822:2015, latest draft is N4100. Your compiler might not have it yet, in which case Boost.FileSystem provides essentially the same.

Share:
30,084
qwe
Author by

qwe

Updated on August 26, 2020

Comments

  • qwe
    qwe over 3 years

    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