std::filesystem::path::replace_extension - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::filesystem::path::replace_extension

From cppreference.com
< cpp‎ | filesystem‎ | path

path& replace_extension( const path& replacement = path() ); | (1) | (since C++17)

Replaces the extension with `replacement` or removes it when the default value of `replacement` is used.

Firstly, if this path has an `extension()`, it is removed.

Then, a dot character is appended if `replacement` is not empty or does not begin with a dot character.

Then `replacement` is appended to the path.

### [edit] Parameters

replacement | - | the extension to replace with

### [edit] Return value

*this

### [edit] Exceptions

(none)

### [edit] Example

Run this code

    #include <iostream>
    #include <filesystem>
    namespace fs = std::filesystem;

    int main()
    {
        fs::path p = "/foo/bar.jpeg";
        std::cout << "Was: " << p << '\n';
        p.replace_extension(".jpg");
        std::cout << "Now: " << p.replace_extension(".jpg") << '\n';
    }

Output:

    Was: "/foo/bar.jpeg"
    Now: "/foo/bar.jpg"

### [edit] See also

extension | returns the file extension path component (public member function) [edit]
filename | returns the filename path component (public member function) [edit]
stem | returns the stem path component (public member function) [edit]
has_extension | checks if the corresponding path element is not empty (public member function)

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/replace_extension&oldid=84949"