我正在尝试从Visual Studio 2012中的directory_iterator获取绝对路径。在早期版本的Boost库中使用native_file_string()是可能的。现在,没有可用的函数成员返回绝对路径。我最终不得不将目录路径与文件名结合在一起:

string         filePath = "C:\\Eigen\\CMakeLists.txt";
string         prefix   = path (filePath).parent_path().string() + "/";
vector<string> fullPaths;  // Full paths of all files in directory.

for (auto i = directory_iterator (path (filePath).branch_path()); i != directory_iterator(); ++i)
    fullPaths.push_back (prefix + i->path().string());

有没有一种方法可以使目录迭代器返回完整路径?在上面的循环中,我尝试了:
auto s1 = i->path().branch_path();
auto s2 = i->path().directory_string();
auto s4 = i->path().file_string();
auto s5 = i->path().root_path();
auto s6 = i->path().parent_path();
auto s7 = i->path().string();
auto s8 = i->path().basename();
auto s9 = i->path().filename();
auto sa = i->path().root_name();
auto sb = i->path().relative_path();

并且它们都不返回完整路径。

编辑:我注意到使用recursive_directory_iterator而不是directory_iterator将产生完整(绝对)路径。这是设计使然吗?如果我不希望递归迭代怎么办?

最佳答案

您可能正在寻找 absolute() ,这是一个非成员函数。

10-06 13:05
查看更多