std::filesy...current_path(3)
β¨ NAME β¨
std::filesystem::current_path β literally std::filesystem::current_path π
okay so bestie, welcome to std::filesy...current_path(3) from the C++ Standard Libary (that's the manual name, periodt) β this is the page about getting and setting your current path, and it's lowkey iconic π
πΈ Synopsis πΈ
Defined in header <filesystem> β here are the overloads, all since C++17:
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 ) noexcept; (4) (since C++17)
Returns or changes the current path. fr fr β¨
1,2)Returns the absolute path of the current working directory, obtained as if (in native format) by POSIXgetcwd. Overload(2)returnspath()if an error occurs.3,4)Changes the current working directory top, as if by POSIXchdir.
π Parameters π
pβ path to change the current working directory to π€οΈecβ out-parameter for error reporting in the non-throwing overloads π
π Return value π
1,2)Returns the current working directory. slay π«Ά3,4)(none) β it's giving void, no return, no problem π
π₯ Exceptions π₯
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. bestie, memory said nope π
1)Throwsstd::filesystem::filesystem_erroron underlying OS API errors, constructed with the OS error code as the error code argument.2)Sets astd::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.3)Throwsstd::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument.4)Sets astd::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π¦ 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. iykyk π
β οΈ bestie, real talk: 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. not me finding out it's main character energy with zero chill π
π§ Example π§
// Run this code
#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';
}
π» Possible output π»
Current path is "D:/local/ConsoleApplication1"
Current path is "E:/Temp"
π See also π
temp_directory_pathβ returns a directory suitable for temporary files (C++17) (function) β literally the temp-file bestie you call when you need a little directory moment πΈ
shoutout to http://cppreference.com and the version/date 2024.06.10 β that's the source, version, and date, and they ate, no cap ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms