Difference between revisions of "cpp/filesystem/absolute"
m (fmt) |
NickHounsome (Talk | contribs) m (Changed example to make sense in the context of the example runner) |
||
Line 37: | Line 37: | ||
int main() | int main() | ||
{ | { | ||
− | + | std::filesystem::path p = "foo.c"; | |
− | std::cout << "Current path is " << fs::current_path() << '\n' | + | std::cout << "Current path is " << fs::current_path() << '\n'; |
− | + | std::cout << "Absolute path for " << p << " is " | |
− | + | << std::filesystem::absolute(p) << '\n'; | |
} | } | ||
|p=true | |p=true | ||
|output= | |output= | ||
− | Current path is " | + | Current path is "/tmp/1622355667.5363104" |
− | Absolute path for " | + | Absolute path for "foo.c" is "/tmp/1622355667.5363104/foo.c" |
}} | }} | ||
Revision as of 22:23, 29 May 2021
Defined in header <filesystem>
|
||
path absolute(const std::filesystem::path& p); path absolute(const std::filesystem::path& p, std::error_code& ec); |
(since C++17) | |
Returns a path referencing the same file system location as p, for which filesystem::is_absolute() is true. The non-throwing overload returns default-constructed path if an error occurs.
Contents |
Parameters
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
.
Exceptions
Any overload not marked noexcept
may throw std::bad_alloc if memory allocation fails.
The overload that does not take a std::error_code& parameter 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.
The overload taking a std::error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur.
Notes
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
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { std::filesystem::path p = "foo.c"; std::cout << "Current path is " << fs::current_path() << '\n'; std::cout << "Absolute path for " << p << " is " << std::filesystem::absolute(p) << '\n'; }
Possible output:
Current path is "/tmp/1622355667.5363104" Absolute path for "foo.c" is "/tmp/1622355667.5363104/foo.c"
See also
(C++17) |
composes a canonical path (function) |
(C++17) |
composes a relative path (function) |