C++ 从目录中读取文件名

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

Read file names from a directory

c++filefile-io

提问by Tony R

I was wondering if there's an easy way in C++ to read a number of file names from a folder containing many files. They are all bitmaps if anyone is wondering.

我想知道 C++ 中是否有一种简单的方法可以从包含多个文件的文件夹中读取多个文件名。如果有人想知道,它们都是位图。

I don't know much about windows programming so I was hoping it can be done using simple C++ methods.

我对 Windows 编程了解不多,所以我希望它可以使用简单的 C++ 方法来完成。

回答by chrish

Boost provides a basic_directory_iteratorwhich provides a C++ standard conforming input iterator which accesses the contents of a directory. If you can use Boost, then this is at least cross-platform code.

Boost 提供了一个basic_directory_iterator,它提供了一个符合 C++ 标准的输入迭代器,它可以访问目录的内容。如果您可以使用Boost,那么这至少是跨平台代码。

回答by John T

I think you're looking for FindFirstFile()and FindNextFile().

我认为您正在寻找FindFirstFile()FindNextFile()

回答by drby

Just had a quick look in my snippets directory. Found this:

刚刚在我的代码片段目录中快速浏览了一下。发现这个:

vector<CStdString> filenames;
CStdString directoryPath("C:\foo\bar\baz\*");

WIN32_FIND_DATA FindFileData; 
HANDLE hFind = FindFirstFile(directoryPath, &FindFileData);

if (hFind  != INVALID_HANDLE_VALUE)
{
    do
    {
        if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
              filenames.push_back(FindFileData.cFileName);
    } while (FindNextFile(hFind, &FindFileData));

    FindClose(hFind);
}

This gives you a vector with all filenames in a directory. It only works on Windows of course.

这为您提供了一个包含目录中所有文件名的向量。当然,它只适用于 Windows。



Jo?o Augustonoted in an answer:

Jo?o Augusto回答中指出:

Don't forget to check after FindClose(hFind)for:

不要忘记检查FindClose(hFind)

DWORD dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES) 
{
  // Error happened        
}

It's especially important if scanning on a network.

如果在网络上扫描,这一点尤其重要。

回答by robermorales

C++17 includes a standardway of achieve that

C++17 包括一种标准的实现方式

http://en.cppreference.com/w/cpp/filesystem/directory_iterator

http://en.cppreference.com/w/cpp/filesystem/directory_iterator

#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
    fs::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::ofstream("sandbox/file2.txt");
    for(auto& p: fs::directory_iterator("sandbox"))
        std::cout << p << '\n';
    fs::remove_all("sandbox");
}

Possible output:

可能的输出:

sandbox/a
sandbox/file1.txt
sandbox/file2.txt

回答by slacy

You could also use the POSIX opendir() and readdir() functions. See this manual pagewhich also has some great example code.

您还可以使用 POSIX opendir() 和 readdir() 函数。请参阅此手册页,其中也有一些很棒的示例代码。

回答by Foredecker

I recommend you can use the native Win32 FindFirstFile()and FindNextFile()functions. These give you full control over how you search for files. This are simple C APIs and are not hard to use.

我建议你可以使用原生的 Win32FindFirstFile()FindNextFile()函数。这些使您可以完全控制搜索文件的方式。这是简单的 C API 并且不难使用。

Another advantage is that Win32 errors are not hidden or made harder to get at due to the C/C++ library layer.

另一个优点是 Win32 错误不会因为 C/C++ 库层而被隐藏或变得更难发现。

回答by Vaibhav Gorde

Another alternative is -

另一种选择是——

  1. system("dir | findstr \".bmp\" > temp.txt ");
  2. Now read temp.txt line by line to get all filenames.
  1. system("dir | findstr \".bmp\" > temp.txt ");
  2. 现在逐行读取 temp.txt 以获取所有文件名。

回答by Meekohi

Why not use glob()?

为什么不使用glob()

glob_t glob_result;
glob("/foo/bar/*",GLOB_TILDE,NULL,&glob_result);
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
  cout << glob_result.gl_pathv[i] << endl;
}