如何在C ++中显示目录?

问题描述

我想查看我所在的目录,例如终端的图片

like this picture

我该如何使用c ++?

解决方法

根据您的C ++口味,对于C ++ 17,您可以使用std::filesystem::current_path,如示例所示。 (请确保使用-std=c++17进行编译,并在某些编译器需要时链接-lstdc++fs

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current path is " << fs::current_path() << '\n';
}

或与操作系统相关的getcwd,可在c ++ 17之前的版本中使用。