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.
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?
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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();
}
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);.