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

血红的双手。 提交于 2019-11-26 17:57:22

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

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