Defined in header
<filesystem> | ||
---|---|---|
path absolute( const std::filesystem::path& p, const std::filesystem::path& base = std::filesystem::current_path() ); | (1) | (since C++17) |
path system_complete(const std::filesystem::path& p); path system_complete(const std::filesystem::path& p, std::error_code& ec); | (2) | (since C++17) |
p
relative to base
according to the following rules: p
has both root name and root directory (e.g. "C:\users"
, then the path is returned, unmodified. p
has a root name not followed by a root directory (e.g. "C:text.txt"
), then base
is inserted between p
's root name and the remainder of p
. Formally, p.root_name() / absolute(base).root_directory() / absolute(base).relative_path() / p.relative_path()
is returned, p
has no root name, but has a root directory (e.g. "/var/tmp/file.txt"
on a POSIX system or "\users\ABC\Document.doc"
on Windows, then the root name of base
, if it has one, is prepended to p
(on a POSIX system, p
is not modified, on a Windows system, "\users\ABC\Document.doc"
becomes "C:\users\ABC\Document.doc"
. Formally, absolute(base).root_name() / p
is returned. p
has no root name and no root directory (e.g. "../file.txt"
, then the entire base
is prepended to p
. Formally, absolute(base) / p
is returned.p
. On POSIX systems, this is equivalent to (1) with the default base
(current_path()
). On Windows systems, each logical drive has its own current working directory, and so if p
is not already absolute and has a root name component (e.g. "E:filename.txt"
, that drive's current working directory is used, which may have been set by an earlier executed program.p | - | path to convert to absolute form |
base | - | path (not necessarily absolute) to serve as the starting location |
ec | - | out-parameter for error reporting in the non-throwing overload |
Returns an absolute (although not necessarily canonical) path formed by combining p
and base
as described above.
std::error_code&
parameter throws filesystem_error on underlying OS API errors, constructed with p
as the first argument, base
as the second 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. This overload has noexcept
specification: noexcept
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 root name of base
is different will usually result in a non-existent path.
#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' << "System complete path for " << p << " is " << fs::system_complete(p) << '\n'; }
Possible output:
Current path is "D:/local/ConsoleApplication1" Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe" System complete path for "C:cl.exe" is "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\cl.exe"
(C++17)
| composes a canonical path (function) |
(C++17)
| composes a relative path (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/filesystem/absolute