std::filesystem::absolute(3)

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

πŸ’– std::filesystem::absolute πŸ’–

okay bestie, welcome to std::filesystem::absolute(3) from the C++ Standard Libary manual. this page is literally about turning a path into an absolute one, and it's giving main character energy ✨ no cap.

πŸ’… NAME πŸ’…

std::filesystem::absolute - std::filesystem::absolute (yes, the name said it twice β€” iconic, no notes πŸ’…)

✨ Synopsis ✨

okay so here's the signature moment, literally:

Defined in header <filesystem>
path absolute( const std::filesystem::path& p );
(1) (since C++17)
path absolute( const std::filesystem::path& p, std::error_code& ec
(2) (since C++17)
);

Returns a path referencing the same file system location as p, for which filesystem::path::is_absolute() is true. that's the vibe check, and it passed πŸ’–

2) This non-throwing overload returns default-constructed path if an error occurs. lowkey the safe one, fr fr.

🌸 Parameters 🌸

what you're feeding it:

  • p β€” path to convert to absolute form
  • ec β€” out-parameter for error reporting in the non-throwing overload

πŸ¦‹ Return value πŸ¦‹

Returns an absolute (although not necessarily canonical) pathname referencing the same file as p. so yes, absolute but not necessarily canonical β€” the distinction is real, bestie 😌

πŸ”₯ Exceptions πŸ”₯

Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. memory said no πŸ’€

  • 1) 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.
  • 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.

πŸŽ€ Notes πŸŽ€

a few notes, periodt:

  • Implementations are encouraged to not consider p not existing to be an error.
  • For POSIX-based operating systems, std::filesystem::absolute(p) is equivalent to std::filesystem::current_path() / p except for when p is the empty path.
  • For Windows, std::filesystem::absolute may be implemented as a call to GetFullPathNameW.

πŸ’œ Example πŸ’œ

run it, bestie:

// Run this code

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    std::filesystem::path p = "foo.c";
    std::cout << "Current path is " << std::filesystem::current_path() << '\n';
    std::cout << "Absolute path for " << p << " is " << fs::absolute(p) << '\n';
}

🌈 Possible output: 🌈

could look like this, no cap:

Current path is "/tmp/1666297965.0051296"
Absolute path for "foo.c" is "/tmp/1666297965.0051296/foo.c"

πŸ‘‘ See also πŸ‘‘

go look at these too, iykyk:

  • canonical β€” composes a canonical path
  • weakly_canonical β€” (function) (C++17)
  • relative β€” composes a relative path
  • proximate β€” (function) (C++17)

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::filesystem::absolute(3) β€” byeee, go make those paths absolute, bestie πŸ’…βœ¨

πŸ“¦ where this page comes from