123456789101112131415161718192021222324252627
#include <string> std::string add_filename_suffix_v1( const std::string& filename, const std::string& suffix ) { auto n = filename.rfind( '.' ); if (filename.find_first_of( "/\\", n ) != filename.npos) return filename + suffix; return filename.substr( 0, n ) + suffix + filename.substr( n ); } #include <filesystem> #include <string> std::string add_filename_suffix_v2( const std::string& filename, const std::string& suffix ) { std::filesystem::path p{ filename }; return p.replace_filename( p.stem().string() + suffix + p.extension().string() ).string(); } #include <iostream> int main() { std::cout << add_filename_suffix_v1( "C:/Users/heomap1/foo.stuff/player.txt", "001" ) << "\n"; std::cout << add_filename_suffix_v2( "C:/Users/heomap1/foo.stuff/player.txt", "002" ) << "\n"; }