Can someone help me

In a past life I could have knocked this out, but I've forgotten about 90 ercent of my c++ from college. I am trying to read lines, which are file names, from a file (lets call it list.txt) then srtip a piece of redundant text from the beginning of each line and write the new text line to a new file with some added verbage to it.

Example: File 1

this_is_redundant_keep_me.xls ->keep_me.xls
this_is_redundant_keep_me_2.xls ->keep_me_2.xls

written to file 2 as:

this_was_kept_keep_me.xls
this_was_kept_keep_me_2.xls

Also I don't know how many lines File 1 (list.txt) could possibly have.

Any help with this would be appreciated. It just seems to be a very redundant task that a C++ program could take care of. Thanks in advance
1
2
3
4
5
string line;
while(getline(fin, line)){//read a line, until there aren't any left
   int p = line.find("->")+2;//find the string after "->"
   fout << "this_was_kept_" << string(line, p) << "\n";//write it
}

Though if you ever need more complex text processing you might want to use perl or python, etc.
This is what I have so far. I may have been unclear in my first post as to the string format in file one. Its format is: this_is_redundant_keep_me.xls
I want to strip the "this_is redundant_" from it
Then write to file 2 "this_was_kept_keep_me.xls"

Right now I have no way of compiling this to test, as I am not on my home PC. Does this look correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main ()
{
        string STRING;
	ifstream infile;
	infile.open ("file1.txt");
        outfile.open("file2.xml");

        while(!infile.eof) // To get you all the lines.
        {
	        getline(infile,STRING); // Saves the line in STRING.
                int p = String.find("this_is_redundant")+2;
	        outfile << "this_was_kept_" << string(STRING, p) <<"Original: " << STRING << "\n"; //writing new file and showing original string
        }

	infile.close();
        outfile.close();

}
Oh. Sorry.

Your current code is not right. string::find returns the position of the first character in the found substring. I added 2 in my code, because I wanted the first char after the string "->" (which has 2 chars in it). So in this code you could add 18, but then you don't need to if you know that there is nothing before "this_is_redundant_" in the string. In that case it is enough just to cut off the first 18 chars with string(String, 18);.
Thank you for your quick response! I will make that change. Hopefully this will compile correctly for me and I can start using it tonight.

Thanks again for ur help!
Topic archived. No new replies allowed.