qwe
qwe

Reputation: 141

Files in directory in C++

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

Upvotes: 14

Views: 30317

Answers (4)

MSalters
MSalters

Reputation: 179907

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.

Upvotes: 4

domachine
domachine

Reputation: 1159

What about the boost library: filesystem. Boost.org

Upvotes: 10

lornova
lornova

Reputation: 6933

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

Upvotes: 5

casablanca
casablanca

Reputation: 70701

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

Upvotes: 30

Related Questions