Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 623
qwe Avatar asked Jul 04 '10 21:07

qwe


People also ask

What is file in directory?

A directory is a unique type of file that contains only the information needed to access files or other directories. As a result, a directory occupies less space than other types of files. File systems consist of groups of directories and the files within the directories.

What is directory in C language?

The directory is a place/area/location where a set of the file(s) will be stored. Subdirectory is a directory inside the root directory, in turn, it can have another sub-directory in it. In C programming language you can list all files and sub-directories of a directory easily.


1 Answers

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);
}
like image 101
casablanca Avatar answered Sep 18 '22 12:09

casablanca