std::filesystem::current_path_C++中文网

定义于头文件 <filesystem>

path current_path();

(1) (C++17 起)

path current_path( std::error_code& ec );

(2) (C++17 起)

void current_path( const std::filesystem::path& p );

(3) (C++17 起)

void current_path( const std::filesystem::path& p, std::error_code& ec );

(4) (C++17 起)

返回或更改当前路径。

1-2) 返回当前工作目录的绝对路径,如同通过 POSIX getcwd 取得(以原生格式)。若错误发生则 (2) 返回 path()

3-4) 更改当前工作目录到 p ,如同通过 POSIX chdir

参数

p - 更改当前工作目录所到的路径
ec - 不抛出重载中报告错误的输出参数

返回值

1-2) 返回当前工作目录。

3-4) (无)

异常

注意

当前工作目录是与进程关联的目录,它被用作相对路径的路径名解析中的起始位置。

许多操作系统返回的当前路径是危险的全局变量。第三方或系统库函数,或另一线程可能未经期待地更改它。

示例

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

可能的输出:

Current path is "D:/local/ConsoleApplication1"