6

I've used this kind of code in my Dev-cpp before:

if((dh = opendir(folder)) !== false){
    while((file = readdir(dh)) !== false){
        // do my stuff
    }
    closedir(dh);
}

But now i am using MSVC++ and i dont know how to add those files there, i tried to copy dirent.h/dir.h/errno.h in there, but it gives another error relating to another included files inside those files ..., and by looking in the files i see mingw stuff there so its compiler related? idk what compiler MSVC++ uses, but is it possible to copypaste those files in MSVC++ and get it working?

I tried to look up some code from MSDN but it was really messed up, so im hoping i could use these functions above...

Bjorn
  • 69,215
  • 39
  • 136
  • 164

2 Answers2

5

I would suggest using FindFirstFile() and FindNextFile().

sample code:

HANDLE hFind;
WIN32_FIND_DATA FindFileData;

if((hFind = FindFirstFile("C:/some/folder/*.txt", &FindFileData)) != INVALID_HANDLE_VALUE){
    do{
        printf("%s\n", FindFileData.cFileName);
    }while(FindNextFile(hFind, &FindFileData));
    FindClose(hFind);
}

This really is better, because i can use "*.txt" etc, makes it much more easier to find some specific filetypes, earlier i had to write own match function for that :D

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

Use boost::filesystem, or std::filesystem if you are using C++17

Axois
  • 1,961
  • 2
  • 11
  • 22