0

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.

floatfil
  • 407
  • 2
  • 10
  • 17
  • hm, you need to find last dot appeareance, make slice and combine new string. where is you stuck? – gaussblurinc Sep 10 '14 at 04:34
  • functions like `substr`, `rfind`, `compare`, `find_last_of` can help you [string class](http://www.cplusplus.com/reference/string/string/) – gaussblurinc Sep 10 '14 at 04:37
  • You need to get the file name without the extension. You could use the code in this answer - http://stackoverflow.com/questions/6646702/get-file-name-without-extension – saraf Sep 10 '14 at 04:37

2 Answers2

4

You just need more practice with strings:

int ii = filename.rfind('.');
filename.insert(ii, "PROCESSED");
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 1
    Absolutely correct. Here is more information: http://www.cplusplus.com/reference/string/string/, and http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html. – FoggyDay Sep 10 '14 at 04:41
  • brrr, hate this kind of answers. OP wants a result, person gives an answser. practice? no. thinking? no. education? no. – gaussblurinc Sep 10 '14 at 04:45
  • @FoggyDay: Thanks, I really should have thought to include a couple of links like that. – Beta Sep 10 '14 at 05:25
  • @gaussblurinc: I see your point, but this is not something the OP could have deduced; if you know the methods, the problem is trivial, and if you don't know them you must reinvent the wheel. Reinventing the wheel can be very educational, but I think in this case the correct answer is "here are the tools that make it trivial, play with them and you will become proficient." – Beta Sep 10 '14 at 05:30
  • @Beta OP use string class and doesn't want to look inside documentation and see ability of the class. And, of course, if you don't think about problem and possible solutions, why you should be succeeded in it? – gaussblurinc Sep 10 '14 at 05:43
-1

Let's keep it simple, I assume fileName is a string.

`#include <sstream>`

using namespace std;
stringstream ss;
fileName.erase(name.end()-4, name.end()); //Extension removal.
ss << name << "PROCESSED.txt";
string newFileName = ss.str();
Javi
  • 3,440
  • 5
  • 29
  • 43
  • 1
    Well, thanks for the downvote but I just answered under the assumptions he gave, so there is no point to downvote. There are better ways, but I find this quite useful and that is why I am including it. – Javi Sep 10 '14 at 06:46