std::filesy...current_path(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ 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. 1,2) Returns the absolute path of the current working directory, obtained as if (in native format) by POSIX getcwd. Overload (2) returns path() if an error occurs.
  2. 3,4) Changes the current working directory to p, as if by POSIX chdir.

πŸŽ€ 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) Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with the OS error code as the error code argument.
  • 2) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
  • 3) Throws std::filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument.
  • 4) Sets a std::error_code& parameter to the OS API error code if an OS API call fails, and executes ec.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