如何在路径规格中工作两个点

How do two dots in work in path specifications

本文关键字:两个 工作 路径      更新时间:2023-10-16

在此示例中

#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::path p = fs::path("..") / ".." / "AppData";//What is it?
    std::cout << "Current path is " << fs::current_path() << 'n'
              << "Canonical path for " << p << " is " << fs::canonical(p) << 'n';
}

什么是"..",它如何工作?

..是父目录,如果您在/home/user/kat中,然后转到..您将在/home/user中。

例如,这样的代码:

fs::path parentPath = fs::current_path() / "..";
std::cout << parentPath << " → " << fs::canonical(parentPath) << std::endl;

将产生此输出(在我的PC上):

"/home/kamil/Pulpit/build/.." → "/home/kamil/Pulpit"