cpp / latest / filesystem / relative.html /

std::filesystem::relative, std::filesystem::proximate

Defined in header <filesystem>
path relative( const std::filesystem::path& p,
               std::error_code& ec );
(1) (since C++17)
path relative( const std::filesystem::path& p,
               const std::filesystem::path& base = std::filesystem::current_path());
path relative( const std::filesystem::path& p,
               const std::filesystem::path& base,
               std::error_code& ec );
(2) (since C++17)
path proximate( const std::filesystem::path& p,
                std::error_code& ec );
(3) (since C++17)
path proximate( const std::filesystem::path& p,
                const std::filesystem::path& base = std::filesystem::current_path());
path proximate( const std::filesystem::path& p,
                const std::filesystem::path& base,
                std::error_code& ec );
(4) (since C++17)
1) Returns relative(p, current_path(), ec)
2) Returns p made relative to base. Resolves symlinks and normalizes both p and base before other processing. Effectively returns std::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base)) or std::filesystem::weakly_canonical(p, ec).lexically_relative(std::filesystem::weakly_canonical(base, ec)), except the error code form returns path() at the first error occurrence, if any.
3) Returns proximate(p, current_path(), ec)
4) Effectively returns std::filesystem::weakly_canonical(p).lexically_proximate(std::filesystem::weakly_canonical(base)) or std::filesystem::weakly_canonical(p, ec).lexically_proximate(std::filesystem::weakly_canonical(base, ec)), except the error code form returns path() at the first error occurrence, if any.

Parameters

p - an existing path
base - base path, against which p will be made relative/proximate
ec - error code to store error status to

Return value

1) p made relative against base.
2) p made proximate against base

Exceptions

The overload that does not take a std::error_code& parameter throws filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument, base as the second 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. Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.

Example

#include <iostream>
#include <filesystem>
 
void show(std::filesystem::path a, std::filesystem::path b)
{
    std::cout << "relative (" << a << "," << b << ") == ";
    std::cout << std::filesystem::relative(a,b) << "\n";
 
    std::cout << "proximate(" << a << "," << b << ") == ";
    std::cout << std::filesystem::proximate(a,b) << "\n";
}
 
int main()
{
    show("/a/b/c","/a/b");
    show("/a/c","/a/b");
    show("c","/a/b");
    show("/a/b","c");
}

Possible output:

relative ("/a/b/c","/a/b") == "c"
proximate("/a/b/c","/a/b") == "c"
relative ("/a/c","/a/b") == "../c"
proximate("/a/c","/a/b") == "../c"
relative ("c","/a/b") == ""
proximate("c","/a/b") == "c"
relative ("/a/b","c") == ""
proximate("/a/b","c") == "/a/b"

See also

(C++17)
represents a path
(class)
(C++17)
composes an absolute path
(function)
(C++17)
composes a canonical path
(function)
converts path to normal form
converts path to relative form
converts path to proximate form
(public member function of std::filesystem::path)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/filesystem/relative