Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::absolute

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

      Returns a path referencing the same file system location asp, for whichfilesystem::path::is_absolute() istrue.

      2) This non-throwing overload returns default-constructed path if an error occurs.

      Contents

      [edit]Parameters

      p - path to convert to absolute form
      ec - out-parameter for error reporting in the non-throwing overload

      [edit]Return value

      Returns an absolute (although not necessarily canonical) pathname referencing the same file asp.

      [edit]Exceptions

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

      1) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument and the OS error code as the error code argument.
      2) 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]Notes

      Implementations are encouraged to not considerp not existing to be an error.

      For POSIX-based operating systems,std::filesystem::absolute(p) is equivalent tostd::filesystem::current_path()/ p except for whenp is the empty path.

      For Windows,std::filesystem::absolute may be implemented as a call toGetFullPathNameW.

      [edit]Example

      Run this code
      #include <filesystem>#include <iostream>namespace fs= std::filesystem; int main(){std::filesystem::path p="foo.c";std::cout<<"Current path is "<<std::filesystem::current_path()<<'\n';std::cout<<"Absolute path for "<< p<<" is "<< fs::absolute(p)<<'\n';}

      Possible output:

      Current path is "/tmp/1666297965.0051296"Absolute path for "foo.c" is "/tmp/1666297965.0051296/foo.c"

      [edit]See also

      composes a canonical path
      (function)[edit]
      composes a relative path
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/absolute&oldid=157936"

      [8]ページ先頭

      ©2009-2025 Movatter.jp