Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      path relative(conststd::filesystem::path& p,
                     std::error_code& ec);
      (1)(since C++17)
      path relative(conststd::filesystem::path& p,
                     conststd::filesystem::path& base=std::filesystem::current_path());
      (2)(since C++17)
      path relative(conststd::filesystem::path& p,

                     conststd::filesystem::path& base,

                     std::error_code& ec);
      (3)(since C++17)
      path proximate(conststd::filesystem::path& p,
                     std::error_code& ec);
      (4)(since C++17)
      path proximate(conststd::filesystem::path& p,
                     conststd::filesystem::path& base=std::filesystem::current_path());
      (5)(since C++17)
      path proximate(conststd::filesystem::path& p,

                     conststd::filesystem::path& base,

                     std::error_code& ec);
      (6)(since C++17)
      1) Returnsrelative(p, current_path(), ec).
      2,3) Returnsp made relative tobase. Resolves symlinks and normalizes bothp andbase before other processing. Effectively returnsstd::filesystem::weakly_canonical(p).lexically_relative(std::filesystem::weakly_canonical(base)) orstd::filesystem::weakly_canonical(p, ec).lexically_relative(std::filesystem::weakly_canonical(base, ec)), except the error code form returnspath() at the first error occurrence, if any.
      4) Returnsproximate(p, current_path(), ec).
      5,6) Effectively returnsstd::filesystem::weakly_canonical(p).lexically_proximate(std::filesystem::weakly_canonical(base)) orstd::filesystem::weakly_canonical(p, ec).lexically_proximate(std::filesystem::weakly_canonical(base, ec)), except the error code form returnspath() at the first error occurrence, if any.

      Contents

      [edit]Parameters

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

      [edit]Return value

      1)p made relative againstcurrent_path().
      2,3)p made relative againstbase.
      4)p made proximate againstcurrent_path().
      5,6)p made proximate againstbase.

      [edit]Exceptions

      Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.

      2,5) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument,base as the second path argument, and the OS error code as the error code argument.
      1,3,4,6) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.

      [edit]Example

      Run this code
      #include <filesystem>#include <iostream> void show(std::filesystem::path x,std::filesystem::path y){std::cout<<"x:\t\t "<< x<<"\ny:\t\t "<< y<<'\n'<<"relative(x, y):  "<< std::filesystem::relative(x, y)<<'\n'<<"proximate(x, y): "<< std::filesystem::proximate(x, y)<<"\n\n";} int main(){    show("/a/b/c","/a/b");    show("/a/c","/a/b");    show("c","/a/b");    show("/a/b","c");}

      Possible output:

      x:               "/a/b/c"y:               "/a/b"relative(x, y):  "c"proximate(x, y): "c" x:               "/a/c"y:               "/a/b"relative(x, y):  "../c"proximate(x, y): "../c" x:               "c"y:               "/a/b"relative(x, y):  ""proximate(x, y): "c" x:               "/a/b"y:               "c"relative(x, y):  ""proximate(x, y): "/a/b"

      [edit]See also

      (C++17)
      represents a path
      (class)[edit]
      (C++17)
      composes an absolute path
      (function)[edit]
      composes a canonical path
      (function)[edit]
      converts path to normal form
      converts path to relative form
      converts path to proximate form
      (public member function ofstd::filesystem::path)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/relative&oldid=158215"

      [8]ページ先頭

      ©2009-2025 Movatter.jp