【问题标题】:How to convert const std::filesystem::directory_entry& to string in c++如何在 C++ 中将 const std::filesystem::directory_entry& 转换为字符串
【发布时间】:2022-01-11 22:44:46
【问题描述】:
#include <filesystem>
#include <string>   
#include <iostream> 

using namespace std; 

int main()
{
    string path = "/logs";
    string str = "";
    for (const auto& file : directory_iterator(path))
    {
        // want to convert file into a string str
    }
}

我可以只使用cout &lt;&lt; file,它的目的是将路径像字符串一样打印到控制台上,但我想将const std::filesystem::directory_entry 转换为字符串以供以后操作。

我该怎么做?

【问题讨论】:

    标签: c++ directory filesystems c++17 directoryentry


    【解决方案1】:

    directory_iterator 迭代一组directory_entry,其中有一个path() 成员。 path 又可转换为 std::string

    所以一路走来

    std::string name = file.path().string();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      相关资源
      最近更新 更多