C++ 目录下的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3176208/
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-08-28 12:15:03  来源:igfitidea点击:

Files in directory in C++

c++windowsfiledirectory

提问by qwe

How to get all files in a given directory using C++ on windows?

如何在 Windows 上使用 C++ 获取给定目录中的所有文件?

Note:
I found methods that use dirent.hbut I need a more standard way...

注意:
我找到了使用的方法,dirent.h但我需要一种更标准的方法......

Thanks

谢谢

回答by casablanca

Use FindFirstFileand related functions. Example:

使用FindFirstFile和相关函数。例子:

HANDLE hFind;
WIN32_FIND_DATA data;

hFind = FindFirstFile("c:\*.*", &data);
if (hFind != INVALID_HANDLE_VALUE) {
  do {
    printf("%s\n", data.cFileName);
  } while (FindNextFile(hFind, &data));
  FindClose(hFind);
}

回答by domachine

What about the boost library: filesystem. Boost.org

boost 库怎么样:文件系统。 Boost.org

回答by lornova

You have to use the FindFirstFilefunction (documented here). This is the standard (and preferred) way in Windows, however it is not portable. The header dirent.hyou have found contains the definition of the standard POSIXfunctions.

您必须使用该FindFirstFile功能(此处记录)。这是Windows 中的标准(和首选)方式,但它不可移植。dirent.h您找到的头文件包含标准 POSIX函数的定义。

For the full code look at this example: Listing the Files in a Directory

有关完整代码,请查看此示例:列出目录中的文件

回答by MSalters

The accepted standard for C++ is described in N1975ISO/IEC TS 18822:2015, latest draft is N4100. Your compiler might not have it yet, in which case Boost.FileSystemprovides essentially the same.

公认的 C++ 标准在N1975ISO/IEC TS 18822:2015 中描述,最新草案是N4100。您的编译器可能还没有,在这种情况下Boost.FileSystem提供的基本相同。