std::filesystem::absolute(3)
π 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 formecβ 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_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - 2) Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π Notes π
a few notes, periodt:
- Implementations are encouraged to not consider
pnot existing to be an error. - For POSIX-based operating systems,
std::filesystem::absolute(p)is equivalent tostd::filesystem::current_path() / pexcept for whenpis the empty path. - For Windows,
std::filesystem::absolutemay be implemented as a call toGetFullPathNameW.
π 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 pathweakly_canonicalβ (function) (C++17)relativeβ composes a relative pathproximateβ (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
- 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