我有一个循环,遍历整个目录,我需要从每个文件获取路径以便使用。
这是我的方法:
#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
时,我可以看到我的文件路径...
std::filesystem::path
来实现这一点... 至于使用完全未知的StreamRAII
的问题,你刚刚平静地提出了这个问题,我可以从路径文档中粘贴相关句子:"......路径可以隐式转换为std::basic_strings
,这使得可以将它们与其他文件 API 一起使用,例如作为 std::ifstream::open 的参数..." - Chef Gladiator