Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::experimental::filesystem::canonical

      From cppreference.com
      <cpp‎ |experimental‎ |fs
       
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<experimental/filesystem>
      path canonical(const path& p,const path& base= current_path());
      (1)(filesystem TS)
      path canonical(const path& p, error_code& ec);
      (2)(filesystem TS)
      path canonical(const path& p,const path& base, error_code& ec);
      (3)(filesystem TS)

      Converts pathp to a canonical absolute path, i.e. an absolute path that has no dot, dot-dot elements or symbolic links.

      Ifp is not an absolute path, the function behaves as if it is first made absolute byabsolute(p, base) orabsolute(p) for(2).

      The pathp must exist.

      Contents

      [edit]Parameters

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

      [edit]Return value

      An absolute path that resolves to the same file asabsolute(p, base) (orabsolute(p) for(2)).

      [edit]Exceptions

      The overload that does not take anerror_code& parameter throwsfilesystem_error on underlying OS API errors, constructed withp 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 anerror_code& parameter sets it to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur. This overload has
      noexcept specification:  
      noexcept
        

      This function is modeled after the POSIXrealpath.

      [edit]Example

      Run this code
      #include <experimental/filesystem>#include <iostream>namespace fs= std::experimental::filesystem; int main(){    fs::path p= fs::path("..")/".."/"AppData";std::cout<<"Current path is "<< fs::current_path()<<'\n'<<"Canonical path for "<< p<<" is "<< fs::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

      represents a path
      (class)[edit]
      composes an absolute path
      converts a path to an absolute path replicating OS-specific behavior
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/experimental/fs/canonical&oldid=158730"

      [8]ページ先頭

      ©2009-2025 Movatter.jp