Namespaces
Variants
Views
Actions

std::filesystem::absolute

From cppreference.com
 
 
 
Defined in header <filesystem>
path absolute(const std::filesystem::path& p);
path absolute(const std::filesystem::path& p, std::error_code& ec);
(1) (since C++17)
1) Returns a path referencing the same file system location as p, for which is_absolute() is true. The non-throwing overload returns default-constructed path if an error occurs.

Contents

[edit] Parameters

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

[edit] Return value

Returns an absolute (although not necessarily canonical) pathname referencing the same file as p

[edit] Exceptions

The overload that does not take a std::error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. 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.

[edit] Notes

It is not an error if absolute results in a path that refers to a non-existent file. In particular, on systems that support root names (e.g. Windows), the result of calling absolute on a relative path that has a root name (e.g. "D:file.txt" when the current working directory is on a different root name, will usually result in a non-existent path.

For POSIX-based operating systems, std::filesystem::absolute(p) is equivalent to std::filesystem::current_path() / p

For Windows, absolute may be implemented as a call to GetFullPathNameW.

[edit] Example

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    fs::path p = "C:cl.exe";
    std::cout << "Current path is " << fs::current_path() << '\n'
              << "Absolute path for " << p << " is " << fs::absolute(p) << '\n';
// actual location: "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe"
}

Possible output:

Current path is "D:/local/ConsoleApplication1"
Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe"

[edit] See also

composes a canonical path
(function) [edit]
composes a relative path
(function) [edit]