首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::current_path

Defined in header <filesystem>

?

?

path current_path();

(1)

(since C++17)

path current_path( std::error_code& ec );

(2)

(since C++17)

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

(3)

(since C++17)

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

(4)

(since C++17)

返回或更改当前路径。

1-2%29返回当前工作目录的绝对路径,该路径似乎是由POSIX获得的。getcwd.%282%29path()如果发生错误。

3-4%29将当前工作目录更改为p,好像是在POSIX克迪尔...

参数

p

-

path to change the current working directory to

ec

-

out-parameter for error reporting in the non-throwing overloads

返回值

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

3-4%29%280%29

例外

1-2%29不需要std::error_code&参数抛文件系统[医]误差在底层OSAPI错误上,使用OS错误代码作为错误代码参数构造。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

3-4%29不需要std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用p作为第一个参数和操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

注记

当前工作目录是用作相对路径的路径解析中的起始位置的目录。单个当前工作目录与整个进程相关联。

当前的工作目录是程序的一个危险的全局状态。各种文件输入/输出相关函数的行为受当前路径值的影响。程序的任何组件(包括各种外部库或其他线程)都可能意外地更改当前路径。

二次

代码语言:javascript
复制
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    std::cout << "Current path is " << fs::current_path() << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
Current path is "D:/local/ConsoleApplication1"

二次

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com