如何使用c+;获取具有特定扩展名的所有文件的列表+;没有<;dirent.h>;在窗户上 如何获取带有C++的特定扩展的所有文件的列表,而不需要在Windows上使用。 h不在VisualStudio2019上工作,是否有其他方法可以获取目录中所有文件的列表

如何使用c+;获取具有特定扩展名的所有文件的列表+;没有<;dirent.h>;在窗户上 如何获取带有C++的特定扩展的所有文件的列表,而不需要在Windows上使用。 h不在VisualStudio2019上工作,是否有其他方法可以获取目录中所有文件的列表,c++,windows,C++,Windows,例如,列表中的 file.txt secode.txt hello.txt h.txt 如果没有,那么我可以让dirent.h在visual studio 2019中工作。如果您有boost,您可以使用boost::filesystem 下面是一个在jpg扩展中获取所有图像的示例 #include <boost/filesystem.hpp> #include <boost/filesystem/path.hpp> #include <vector> #in

例如,列表中的

file.txt
secode.txt
hello.txt
h.txt

如果没有,那么我可以让dirent.h在visual studio 2019中工作。

如果您有boost,您可以使用boost::filesystem 下面是一个在jpg扩展中获取所有图像的示例

#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <vector>
#include <string>
using namespace boost::filesystem;
using std::string;
using std::vector;
    void GetAllImageFromDir(path& dir, vector<path>& all_dir_images)
{
    string str(".jpg");             // store paths,
    vector<path> all_dir_files;

    std::copy(directory_iterator(dir), directory_iterator(), back_inserter(all_dir_files));

    for (int k = 0; k < all_dir_files.size(); ++k )
    {

        if (all_dir_files[k].filename_is_dot() || all_dir_files[k].filename_is_dot_dot() || !is_regular_file(all_dir_files[k]))
        {
            continue;
        }
        else
        {
            if (is_regular_file(all_dir_files[k]))
            {
                string extension = boost::filesystem::extension(all_dir_files[k].string());

                    if (extension.compare(str) == 0)
                    {
                        all_dir_images.push_back(all_dir_files[k]);
                    }
            }
        }
    }
}
#包括
#包括
#包括
#包括
使用名称空间boost::filesystem;
使用std::string;
使用std::vector;
void GetAllImageFromDir(路径和方向、向量和所有方向图像)
{
string str(“.jpg”);//存储路径,
矢量所有_dir_文件;
复制(目录迭代器(dir)、目录迭代器()、返回插入器(所有目录迭代器文件));
对于(int k=0;k
如果您有boost,您可以使用boost::filesystem 下面是一个在jpg扩展中获取所有图像的示例

#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <vector>
#include <string>
using namespace boost::filesystem;
using std::string;
using std::vector;
    void GetAllImageFromDir(path& dir, vector<path>& all_dir_images)
{
    string str(".jpg");             // store paths,
    vector<path> all_dir_files;

    std::copy(directory_iterator(dir), directory_iterator(), back_inserter(all_dir_files));

    for (int k = 0; k < all_dir_files.size(); ++k )
    {

        if (all_dir_files[k].filename_is_dot() || all_dir_files[k].filename_is_dot_dot() || !is_regular_file(all_dir_files[k]))
        {
            continue;
        }
        else
        {
            if (is_regular_file(all_dir_files[k]))
            {
                string extension = boost::filesystem::extension(all_dir_files[k].string());

                    if (extension.compare(str) == 0)
                    {
                        all_dir_images.push_back(all_dir_files[k]);
                    }
            }
        }
    }
}
#包括
#包括
#包括
#包括
使用名称空间boost::filesystem;
使用std::string;
使用std::vector;
void GetAllImageFromDir(路径和方向、向量和所有方向图像)
{
string str(“.jpg”);//存储路径,
矢量所有_dir_文件;
复制(目录迭代器(dir)、目录迭代器()、返回插入器(所有目录迭代器文件));
对于(int k=0;k
使用C++17,这在没有boost的情况下也很容易实现。我使用VS2019测试了下面的示例

请参见下面的示例。阅读完整的文件列表只需很短的一行:

std::vector fileList (fs::directory_iterator(startPath), {});
std::vector
使用范围构造函数

我们可以在没有模板参数的情况下定义std::vector。编译器可以从给定的函数参数推断参数。此功能称为CTAD(“类模板参数推断”)

此外,您可以看到,我没有明确地使用“end()”-迭代器

此迭代器将从空括号内的初始值设定项列表中构造,类型正确,因为由于std::vector构造函数需要,它将被推断为与第一个参数的类型相同

当然,您也可以使用
fs::recursive\u directory\u迭代器检查子目录

因此,找到扩展非常简单。当然,您也可以检查其他文件/路径属性。没问题

请参阅完整的工作示例:

#include <iostream>
#include <filesystem>
#include <vector>
#include <iterator>

namespace fs = std::filesystem;

int main() {

    // The start path
    const fs::path startPath{ "C:\\temp\\" };
    // And the extension to look for
    const std::string extension{ ".txt" };

    // Get all directory entries into our fileList vector
    std::vector fileList (fs::directory_iterator(startPath), {});

    // Evaluate. Go through all directory entries
    for (const fs::directory_entry& de : fileList) {

        // Get path as string
        std::string p{ de.path().string() };

        // Check if it ends with the given extension
        if (p.substr(p.size() - extension.size(), extension.size()) == extension)
            std::cout << p << "\n";
    }
    return 0;
}

使用C++17,这在没有boost的情况下也很容易实现。我使用VS2019测试了下面的示例

请参见下面的示例。阅读完整的文件列表只需很短的一行:

std::vector fileList (fs::directory_iterator(startPath), {});
std::vector
使用范围构造函数

我们可以在没有模板参数的情况下定义std::vector。编译器可以从给定的函数参数推断参数。此功能称为CTAD(“类模板参数推断”)

此外,您可以看到,我没有明确地使用“end()”-迭代器

此迭代器将从空括号内的初始值设定项列表中构造,类型正确,因为由于std::vector构造函数需要,它将被推断为与第一个参数的类型相同

当然,您也可以使用
fs::recursive\u directory\u迭代器检查子目录

因此,找到扩展非常简单。当然,您也可以检查其他文件/路径属性。没问题

请参阅完整的工作示例:

#include <iostream>
#include <filesystem>
#include <vector>
#include <iterator>

namespace fs = std::filesystem;

int main() {

    // The start path
    const fs::path startPath{ "C:\\temp\\" };
    // And the extension to look for
    const std::string extension{ ".txt" };

    // Get all directory entries into our fileList vector
    std::vector fileList (fs::directory_iterator(startPath), {});

    // Evaluate. Go through all directory entries
    for (const fs::directory_entry& de : fileList) {

        // Get path as string
        std::string p{ de.path().string() };

        // Check if it ends with the given extension
        if (p.substr(p.size() - extension.size(), extension.size()) == extension)
            std::cout << p << "\n";
    }
    return 0;
}

如果您的编译器足够新,请使用。如果没有,您可能需要或与匹配的
FindNextFile
一起使用。有关
版本,请参阅。页面底部有一个很好的用法示例。
FindFirstFile
兄弟在给定的链接中有详细的文档记录,并附有示例。这是否回答了您的问题?提到的dupe不是指扩展。被愚弄者接受的答案是使用“dirent.h”。圣杯守护者错误判断的另一个例子。如果您的编译器足够新,请使用。如果没有,您可能需要或与匹配的
FindNextFile
一起使用。有关
版本,请参阅。页面底部有一个很好的用法示例。
FindFirstFile
兄弟在给定的链接中有详细的文档记录,并附有示例。这是否回答了您的问题?提到的dupe不是指扩展。被愚弄者接受的答案是使用“dirent.h”。圣杯守护者错误判断的另一个例子。你能展示你在代码中使用的所有库吗?我用我记忆中的编辑如果不编译,我将检查丢失的东西开源文件“boost/filesystem.hpp”不能运行