问题
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...
回答1:
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
回答2:
Use boost::filesystem
, or std::filesystem
if you are using C++17
来源:https://stackoverflow.com/questions/883594/microsoft-visual-studio-opendir-and-readdir-how