GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::filesystem::path::replace_extension(3) C++ Standard Libary std::filesystem::path::replace_extension(3)

std::filesystem::path::replace_extension - std::filesystem::path::replace_extension


path& replace_extension( const path& replacement = path() ); (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 from the generic-format view
of the pathname.


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


Then replacement is appended as if by operator+=(replacement).


replacement - the extension to replace with


*this


May throw implementation-defined exceptions.


The type of replacement is std::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.

// Run this code


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


int main()
{
std::cout << "Path: Ext: Result:\n" << std::left;
for (const auto& [path, extension] : {
std::make_pair( "/foo/bar.jpg", ".png" ),
std::make_pair( "/foo/bar.jpg", "png" ),
std::make_pair( "/foo/bar.jpg", "." ),
std::make_pair( "/foo/bar.jpg", "" ),
std::make_pair( "/foo/bar." , "png" ),
std::make_pair( "/foo/bar" , ".png" ),
std::make_pair( "/foo/bar" , "png" ),
std::make_pair( "/foo/bar" , "." ),
std::make_pair( "/foo/bar" , "" ),
std::make_pair( "/foo/." , ".png" ),
std::make_pair( "/foo/." , "png" ),
std::make_pair( "/foo/." , "." ),
std::make_pair( "/foo/." , "" ),
std::make_pair( "/foo/" , ".png" ),
std::make_pair( "/foo/" , "png" ), })
{
fs::path p = path, e = extension;
std::cout << std::setw(18) << p << std::setw(11) << e;
p.replace_extension(e);
std::cout << p << '\n';
}
}


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"


extension returns the file extension path component
(public member function)
filename returns the filename path component
(public member function)
stem returns the stem path component (filename without the final extension)
(public member function)
has_extension checks if the corresponding path element is not empty
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.