Find replace strings in file in loop
Dear Sir,
I am trying to find and replace some of the strings which contains a combinations of string and integers. So I used stringstream to convert it to strings. I am taking input for these strings from input.txt file , used these input in input1.txt and replacing strings from this (input1.txt) file and outputing output.txt with changes in input1.txt to output.txt.
I am giving explaination to you as follows :
input.txt
Enter_the_number_of_names
2
Enter_the_name
Enter_the_range_of_points
Shri
600001
600003
Proactive
600004
600006
input1.txt
$POINT ID = 600001
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
My program is
Code:
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<sstream>
using namespace std;
struct NVH
{
char name[200];
int points[200];
};
int main()
{
ifstream file;
file.open("input.txt");
ifstream pch;
pch.open("input1.txt");
char input2[300],name2[300];
ofstream pchout;
pchout.open("output.txt");
struct NVH n[200];
long int i,j,k,l,m,p,a,b,c,x,y,z,r,s,d,f;
float e;
long int g,h,t;
int u=900,v=400,w=700;
char nam[300],name[300],input[300],name1[300];
string line;
file>>nam;
file>>c;
file>>nam;
file>>nam;
for(i=0;i<c;i++)
{
file>>n[i].name;
string str("$POINT ID = ");
string replace;
replace=n[i].name;
for(p=0;p<2;p++)
{
file>>n[i].points[p];
}
x=n[i].points[0];
y=n[i].points[1];
for( j=x;j<y;j++)
{
std::stringstream tc;
tc<<j;
string integer=tc.str();
string search;
search=str+integer;
while(!pch.eof())
{
getline(pch,line);
assert(search!=replace);
string::size_type pos=0;
while ((pos=line.find(search,pos))!=string::npos)
{
line.replace(pos+search.size(),search.size(),replace);
pos++;
}
pchout<<line<<endl;
}
}
}
file.close();
pch.close();
pchout.close();
return 0;
}
With above program I am getting folowing output.
output.txt
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002
I don't go to college.
It is tedious program
$POINT ID = 6000003
Shridhar Ram Rahim
$POINT ID = 6000004
Very Easy Program
$POINT ID = 6000005
Life is nice
$POINT ID = 6000006
Here my problem starts. Acually I want folowing output
output.txt
$POINT ID = 600001Shri
I went to college
$POINT ID = 600002Shri
I don't go to college.
It is tedious program
$POINT ID = 6000003Shri
Shridhar Ram Rahim
$POINT ID = 6000004Proactive
Very Easy Program
$POINT ID = 6000005Proactive
Life is nice
$POINT ID = 6000006Proactive
MY PRBLEM IS
I could able to read input.txt as input . I used this input to find from input1.txt and with find replace functions tried to give output.txt .
But I am getting only $POINT ID = 600001Shri as correct output and not changing any further changes. I could give correct required output if I don't use file management section but I use files I did not get correct ouput.
Actullay I want to find the strings which contains integers and append names(e.g. Shri) ahead of it throught the file. I could make it possible for only first point(6000001) but could not repeat it for next numbers(6000002 and above). And This is my main problem.I think I could not use File Management Section of program correctly.
Thanks in Advance