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...
I would suggest using FindFirstFile() and FindNextFile().
Here is the MSDN example which does exactly what you want.
If you need a quick drop in-replacement, you can also use:
http://www.two-sdg.demon.co.uk/curbralan/code/dirent/dirent.html
Simple class I found that uses the POSIX API for Win32
Great, it wasnt so hard after all, i had to use do-while loop though:
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
The answer to this question depends a lot on the target platform you're compiling for.
MSVC++ is a compiler so I'm going to guess you're trying to perform similar actions as opendir() and readdir() on a Windows Platform. In that case, have a look at the following resources:
The second link is a page of MSDN which lists all of the functions available on the Windows Platform API to interact with the file system. You will find that most operations with files in Windows start with a call to the CreateFile function.
Another, possibly much better drop-in replacement: https://github.com/tronkko/dirent
来源:https://stackoverflow.com/questions/883594/microsoft-visual-studio-opendir-and-readdir-how