我有一个遍历整个目录的循环,我需要从每个文件获取路径才能使用它。
这是我的方法
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
for (const auto & entry : fs::directory_iterator(sourceDir))
{
if (fs::is_directory(entry))
continue;
StreamRAII iStream{ entry, StreamMode::READ_BINARY };
}
问题是 StreamRAII
构造函数必须将 std::string
作为其第一个参数
StreamRAII(const std::string& filename, const char *mode);
但是这个 const auto & entry : fs::directory_iterator(sourceDir)
, entry
是 derectory_entry
类型。
问题是,如何获取我的文件的路径?如何将 derectory_entry
转换为 std::string
或 char[]
?
P.S. 当我使用 cout
时,我可以看到我的文件的路径...
最佳答案
derectory_entry
有一个path()
返回 std::filesystem::path
的函数保存文件的路径名。您可以使用它的 string()
从该路径获取 std::string
的函数。这将使您的代码看起来像
StreamRAII iStream{ entry.path().string(), StreamMode::READ_BINARY };
关于c++ - 如何从文件系统获取 std::string 或 char[]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376463/