std::filesystem::canonical, std::filesystem::weakly_canonical - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::filesystem::canonical, std::filesystem::weakly_canonical

From cppreference.com
< cpp‎ | filesystem

Defined in header `<filesystem>` |  |
path canonical( const std::filesystem::path& p, const std::filesystem::path& base = std::filesystem::current_path() ); | (1) | (since C++17)
path canonical( const std::filesystem::path& p, std::error_code& ec ); | (2) | (since C++17)
path canonical( const std::filesystem::path& p, const std::filesystem::path& base, std::error_code& ec ); | (3) | (since C++17)
path weakly_canonical(const std::filesystem::path& p); | (4) | (since C++17)
path weakly_canonical(const std::filesystem::path& p, std::error_code& ec); | (5) | (since C++17)

1-3) Converts path `p` to a canonical absolute path, i.e. an absolute path that has no dot, dot-dot elements or symbolic links. If `p` is not an absolute path, the function behaves as if it is first made absolute by absolute(p, base) or absolute(p) for (2). The path `p` must exist.
4-5) Returns a path composed by `operator/=` from the result of calling `canonical()` without a base argument and with a path argument composed of the leading elements of `p` that exist (as determined by `status(p)` or `status(p, ec)`), if any, followed by the elements of `p` that do not exist, if any. The resulting path is in normal form.

### [edit] Parameters

p | - | a path which may be absolute or relative to `base`, and which must be an existing path
base | - | base path to be used in case `p` is relative
ec | - | error code to store error status to

### [edit] Return value

1-3) An absolute path that resolves to the same file as absolute(p, base) (or absolute(p) for (2)).
4-5) A normal path of the form `canonical(x)/y`, where x is a path composed of the longest leading sequence of elements in p that exist, and y is a path composed of the remaining trailing non-existent elements of 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, `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

### [edit] Notes

The function `canonical()` is modeled after the POSIX realpath.

The function `weakly_canonical()` was introduced to simplify operational semantics of `relative()`.

### [edit] Example

Run this code

    #include <iostream>
    #include <filesystem>
    namespace fs = std::filesystem;
    int main()
    {
        fs::path p = fs::path("..") / ".." / "AppData";
        std::cout << "Current path is " << fs::current_path() << '\n'
                  << "Canonical path for " << p << " is " << canonical(p) << '\n';
    }

Possible output:

    Current path is "C:\Users\abcdef\AppData\Local\Temp"
    Canonical path for "..\..\AppData" is "C:/Users\abcdef\AppData"

### [edit] See also

path (C++17) | represents a path (class) [edit]
absolutesystem_complete (C++17)(C++17) | composes an absolute path converts a path to an absolute path replicating OS-specific behavior (function) [edit]
relativeproximate (C++17) | composes a relative path (function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/canonical&oldid=88139"
Category:

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template