visual-studio Microsoft Visual Studio:opendir() 和 readdir(),如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/883594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:31:01  来源:igfitidea点击:

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

visual-studiovisual-c++readdir

提问by

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

我以前在我的 Dev-cpp 中使用过这种代码:

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?

但现在我正在使用 MSVC++,我不知道如何在那里添加这些文件,我试图在那里复制 dirent.h/dir.h/errno.h,但它给出了另一个与这些文件中的另一个包含文件相关的错误.. .,通过查看文件,我看到了 mingw 的东西,所以它的编译器相关?idk MSVC++ 使用什么编译器,但是否可以将这些文件复制粘贴到 MSVC++ 中并使其工作?

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

我试图从 MSDN 中查找一些代码,但它真的很混乱,所以我希望我可以使用上面的这些功能......

回答by

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

我建议使用FindFirstFile()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

这确实更好,因为我可以使用“*.txt”等,这样可以更容易地找到某些特定的文件类型,之前我不得不为此编写自己的匹配函数:D

回答by Philip Taylor

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

使用boost::filesystem, 或者std::filesystem如果您使用的是 C++17