【问题标题】:Is there a standard way to do findfirst, findnext with gcc on linux using stl?有没有一种标准的方法来使用 stl 在 linux 上使用 gcc 进行 findfirst、findnext?
【发布时间】:2010-09-19 02:57:40
【问题描述】:

我似乎无法在 gcc for Linux 上找到 _findfirst / findfirst、_findnext / findnext API,如果它包含在其中,我实际上宁愿使用标准模板库 (STL)。

有谁知道有哪些 API 可用于在 Linux for C++ (gcc) 下列出目录中的文件?

【问题讨论】:

    标签: c++ linux gcc stl file


    【解决方案1】:

    它不是 C++ 风格的 API,但您找不到的 API(DOS/Windows 风格的 findfirst/findnext 的 Linux/Unix 对应)是 opendir/readdir/closedir。

    使用 opendir/readdir/closedir 的主要优点是您不需要任何额外的库(它是您已经在使用的 C 库的一部分)。事实上,Boost 文件系统库使用 opendir/readdir/closedir 来获取目录中的文件列表。

    参考资料:

    【讨论】:

      【解决方案2】:

      查看Boost.Filesystem 库。

      特别是basic_directory_iterator

      【讨论】:

        【解决方案3】:

        STL 还没有列出目录中文件的功能。但它确实具有打开您已经知道的文件的功能。

        除了Boost.Filesystem,还有STLSoft

        【讨论】:

          【解决方案4】:

          自 C++17 起,标准库包含 std::filesystem,其源代码位于 Boost.Filesystem。如今std::filesystem::directory_iterator 是显而易见的选择,因为它独立于平台,提供比_findfirst/findnext/opendir/readdir/closedir 更好的抽象并且不引入任何依赖项。如果您不能使用符合 C++17 的编译器,请暂时使用 Boost,稍后再切换。

          【讨论】:

            猜你喜欢
            • 2010-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-17
            • 1970-01-01
            • 1970-01-01
            • 2010-11-08
            相关资源
            最近更新 更多