Microsoft Visual Studio: opendir() and readdir(), how?

隐身守侯 提交于 2019-12-17 04:07:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!