C++ examples for File Stream:File Operation
Replacing a File Extension
#include <iostream> #include <string> using std::string; void replaceExt(string& s, const string& newExt) { string::size_type i = s.rfind('.', s.length()); if (i != string::npos) { s.replace(i+1, newExt.length(), newExt); }/*from w ww . j av a 2 s. co m*/ } int main(int argc, char** argv) { string path = "c:/abc/def"; replaceExt(path, "foobar"); std::cout << "The new name is \"" << path << "\"\n"; }