std::filesystem::current_path

Header: <filesystem>

Returns or changes the current path.

# Declarations

path current_path();

(since C++17)

path current_path( std::error_code& ec );

(since C++17)

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

(since C++17)

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

(since C++17)

# Parameters

# Notes

The current working directory is the directory, associated with the process, that is used as the starting location in pathname resolution for relative paths.

The current path as returned by many operating systems is a dangerous global variable. It may be changed unexpectedly by third-party or system library functions, or by another thread.

# Example

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

# See also