Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path::replace_extension

      From cppreference.com
      <cpp‎ |filesystem‎ |path
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      std::filesystem::path
      Member types
      Member constants
      Member functions
      Path decomposition
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
       
      path& replace_extension(const path& replacement= path());
      (since C++17)

      Replaces the extension withreplacement or removes it when the default value ofreplacement is used.

      Firstly, if this path has anextension(), it is removed from the generic-format view of the pathname.

      Then, a dot character is appended to the generic-format view of the pathname, ifreplacement is not empty and does not begin with a dot character.

      Thenreplacement is appended as if byoperator+=(replacement).

      Contents

      [edit]Parameters

      replacement - the extension to replace with

      [edit]Return value

      *this

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [edit]Notes

      The type ofreplacement isstd::filesystem::path even though it is not intended to represent an object on the file system in order to correctly account for the filesystem character encoding.

      [edit]Example

      Run this code
      #include <filesystem>#include <iomanip>#include <iostream>#include <utility> int main(){constint width1{18}, width2{11};// columns' width std::cout<<std::left<<std::setw(width1)<<"Path:"<<std::setw(width2)<<"Ext:"<<"Result:\n";for(constauto&[p, e]:{std::make_pair("/foo/bar.jpg",".png"),{"/foo/bar.jpg","png"},{"/foo/bar.jpg","."},{"/foo/bar.jpg",""},{"/foo/bar.","png"},{"/foo/bar",".png"},{"/foo/bar","png"},{"/foo/bar","."},{"/foo/bar",""},{"/foo/.",".png"},{"/foo/.","png"},{"/foo/.","."},{"/foo/.",""},{"/foo/",".png"},{"/foo/","png"}}){std::filesystem::path path{p}, ext{e};std::cout<<std::setw(width1)<< path<<std::setw(width2)<< ext;        path.replace_extension(ext);std::cout<< path<<'\n';}}

      Output:

      Path:             Ext:       Result:"/foo/bar.jpg"    ".png"     "/foo/bar.png""/foo/bar.jpg"    "png"      "/foo/bar.png""/foo/bar.jpg"    "."        "/foo/bar.""/foo/bar.jpg"    ""         "/foo/bar""/foo/bar."       "png"      "/foo/bar.png""/foo/bar"        ".png"     "/foo/bar.png""/foo/bar"        "png"      "/foo/bar.png""/foo/bar"        "."        "/foo/bar.""/foo/bar"        ""         "/foo/bar""/foo/."          ".png"     "/foo/..png""/foo/."          "png"      "/foo/..png""/foo/."          "."        "/foo/..""/foo/."          ""         "/foo/.""/foo/"           ".png"     "/foo/.png""/foo/"           "png"      "/foo/.png"

      [edit]See also

      returns the file extension path component
      (public member function)[edit]
      returns the filename path component
      (public member function)[edit]
      returns the stem path component (filename without the final extension)
      (public member function)[edit]
      checks if the corresponding path element is not empty
      (public member function)
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/replace_extension&oldid=160020"

      [8]ページ先頭

      ©2009-2026 Movatter.jp