Replacing a File Extension - C++ File Stream

C++ examples for File Stream:File Operation

Description

Replacing a File Extension

Demo Code

#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";
}

Result


Related Tutorials