Convert integers to strings and use them for find replace in file

Dear Sir,

I am tring to convert integer to string and using string for further find and replace operations. While doing so I am using for loop to for integers and then wants to store all these integers in strings.

But I did not succeed. But If I use only one integer I can convert it into string and use it for further operations.

I am giving my part of program as follows;

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;
}

The program basic is as follows:
As I want to find certain numbers e.g suppose ranging from 60001 to 60010 at various locations in the file and then replacing them with some other strings.

Can anyone help me

Thank you
Dear Sir,

As stated above I am just briefing my program to you

The program basic is like I have one input file inpt.txt
Shri
600001
600003
Proactive
600004
600006

First I am reading this input file. Now I have other input1.txt file as

600001
I went to college
600002
I don't go to college.
It is tedious program
6000003
Shridhar Ram Rahim
6000004
Very Easy Program
6000005
Life is nice
6000006


Now I want to take input from input.txt use it in input1.txt and produce the output.txt file as foloows:

600001:shri
I went to college
600002:shri
I don't go to college.
It is tedious program
6000003:shri
Shridhar Ram Rahim
6000004:Proactive
Very Easy Program
6000005:Proactive
Life is nice
6000006:Proactive


Please help me as I could able to produce output.txt fie but I could not repeat it for other numbers
Thank you
Show more code... explain your problem better.

You want to take inpt.txt data and input1.txt data and mash data together and make a new file called output.txt. I don't really understand your system of how you're going to actually know where to put data from inpt.txt and input1.txt to actually logically come up with output.txt

I thought for a second that shri.... ok I get it now. Your numbers are messed up in inpt.txt though.

I don't get how your generating these magical numbers.... Why do you need them?

Do you want to parse a file for a number then match that number in another file resulting in modified output that goes in a completely different file?
Last edited on
Dear Sir,

As per your wish I am just giving my code to you.

My input.txt

Enter_the_number_of_names
2
Enter_the_name
Enter_the_range_of_points
Shri
600001
600003
Proactive
600004
600006



My 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

#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;

}



I am getting output.txt by running the program as

$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




BUT I want output.txt as

$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



Please help me as I almost done the correct program but I think Only problem as you stated I could not repeat the loop in file.


Thank you in advance



Dear Sir,

Anyone can give me the solution of my above defined problem.

I have some problem with file management section which I am finding difficult to solve

Thank you
Topic archived. No new replies allowed.