Search code examples
c++filenames

Adding to the name of a file


I'm working on an application that processes text files, and I want to create a new file with a similar name to that file, but slightly modified.

So for instance, I have a function that takes a string fileName as a parameter and creates a new file with the word "PROCESSED" added before ".txt."

so if fileName = "testFile.txt" the new file should be named "testFilePROCESSED.txt"

string newFile = filename + "PROCESSED"; obviously doesn't work since the filename would be "testFile.txtPROCESSED" in this case.


Solution

  • You just need more practice with strings:

    int ii = filename.rfind('.');
    filename.insert(ii, "PROCESSED");