Difference between revisions of "cpp/filesystem/absolute"
(matched parenthesis) |
(p0492r2's rewrite of absolute(). RIP, system_complete) |
||
Line 1: | Line 1: | ||
− | {{cpp/filesystem/title|absolute | + | {{cpp/filesystem/title|absolute}} |
{{cpp/filesystem/navbar}} | {{cpp/filesystem/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | filesystem}} | {{dcl header | filesystem}} | ||
{{dcl | since=c++17 |num=1|1= | {{dcl | since=c++17 |num=1|1= | ||
− | path absolute | + | path absolute(const std::filesystem::path& p); |
− | + | path absolute(const std::filesystem::path& p, std::error_code& ec); | |
− | + | ||
− | + | ||
− | + | ||
− | path | + | |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Returns | + | @1@ Returns a path referencing the same file system location as {{c|p}}, for which {{ltt|cpp/filesystem/path/is_absrel|is_absolute()}} is {{c|true}}. The non-throwing overload returns default-constructed path if an error occurs. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
{{par | p | path to convert to absolute form}} | {{par | p | path to convert to absolute form}} | ||
− | |||
{{par | ec | out-parameter for error reporting in the non-throwing overload }} | {{par | ec | out-parameter for error reporting in the non-throwing overload }} | ||
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | Returns an absolute (although not necessarily canonical) | + | Returns an absolute (although not necessarily canonical) pathname referencing the same file as {{tt|p}} |
===Exceptions=== | ===Exceptions=== | ||
− | {{cpp/filesystem/error_handling|p | + | {{cpp/filesystem/error_handling|p}} |
===Notes=== | ===Notes=== | ||
− | + | It is not an error if {{tt|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 {{tt|absolute}} on a relative path that has a root name (e.g. {{c|"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, {{c|std::filesystem::absolute(p)}} is equivalent to {{c|std::filesystem::current_path() / p}} | ||
+ | |||
+ | For Windows, {{tt|absolute}} may be implemented as a call to [https://msdn.microsoft.com/en-us/library/windows/desktop/aa364963(v=vs.85).aspx GetFullPathNameW]. | ||
===Example=== | ===Example=== | ||
Line 45: | Line 39: | ||
fs::path p = "C:cl.exe"; | fs::path p = "C:cl.exe"; | ||
std::cout << "Current path is " << fs::current_path() << '\n' | std::cout << "Current path is " << fs::current_path() << '\n' | ||
− | << "Absolute path for " << p << " is " << fs::absolute(p) << '\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" | |
} | } | ||
|p=true | |p=true | ||
Line 52: | Line 46: | ||
Current path is "D:/local/ConsoleApplication1" | Current path is "D:/local/ConsoleApplication1" | ||
Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe" | Absolute path for "C:cl.exe" is "C:/local/ConsoleApplication1/cl.exe" | ||
− | |||
}} | }} | ||
Revision as of 13:52, 20 April 2017
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) |
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
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.
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"
See also
(C++17) |
composes a canonical path (function) |
(C++17) |
composes a relative path (function) |