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 in advance

Recommended Answers

All 9 Replies

Are you saying?:
1) you have a file of numbers
2) you want to find and replace numbers (or ranges) in that file that are input by the user
or
3) you want to find and replace numbers in that file that are given to it by the program

For starters, for the first value of j, you go all the way through the file, replacing occurrences, but then don't save the file or re-initialize pch.

Instead, it may be more efficient (depending on the number of values of j) to read each line from the file, then loop over j, replacing each occurrence of each number in turn, and write the line out to a new file. Then move on to the next line of the input file.

Also note that you will get unexpected results if x is 2 and y is 30 (for example). I leave it to you to understand what those results will be, and how to fix the code accordingly, though for your use cases it might not come up.

Finally, please, please, please (and this goes for everyone!) surround your source code with CODE blocks! Simply select it all with the mouse, and click the [ CODE ] icon at the top of the editor. Does nobody read the clearly-titled "Read this before posting" thread? :-/

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 I could not repeat it for other points.

Thank you in advance

You are still not using [ CODE ] tags.

Then, read out loud what your program is doing:
for each name in input.txt you do:
+ read the name, read the start integer, read the end integer
+ for each integer in the range you do:
+ + read a line from input1.txt
+ + append the name to the line if the integer matches
+ + write out the line
+ + (do not reset the input-stream for input1.txt -> the remaining integers do nothing because pch.eof() is always true after the first integer)

You have several logic flaws. First, you are writing out the line from input1.txt whether or not the match succeeded and name was appended. Second, you are not appending the name onto the line for all possible matched integers before outputting. Third, if your $POINT ID aren't sorted correctly, you could end up with interleaved names in the correct output, but your looping strategy won't handle that.

You're off to a good start by reading input.txt into structures, but do that in a separate loop first, before you look at input1.txt.

Then re-read what I wrote previously. Your outer loop should be over lines in input1.txt: for each line, you need to determine which point ID is specified (if any), and if there is one, which name has an ID range containing that ID value (if any), and if there is matching name, append that name to the line, then write it out.

P.S., If you don't use [ CODE ] tags next time, I won't respond again.

Also, your original post said you wished to replace numbers with other text, but your program actually replaces the (up to) N characters immediately following the number. If you want to insert the names after the numbers, then use the correct function (not replace()). If you want to append the names at the end of the line, then do that.

dear Sir,
I am giving my code. I fill I can able to append the names as you stated besides I don't have the problems with other queries.

Only I can not handle the file management section correctly.

Please help me

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

}

Please format your code at all time so we can read it. This has been mentioned before.

You are correct, you are not handling the file-management aspect correctly, I've told you exactly why your logic (currently in lines 72-100) is flawed, and how to reorganize it so that it does what you expect. I'm not going to tell you again, please read what I've already written previously in this thread. If there's something in there you don't understand, feel free to ask, but don't post the same code over and over and ask us to help you.

I am being stuck up at the same point in File Section and could not repeat the loop or pointer for other points and names.
So if i get a piece of code I can further try to resolve my problem.

Dear Sir,

The code I have posted is formatted .I am briefing my problem again
inpt.txt is a user defined names and range of numbers which the user can change as per his requirement. The input1.txt is a file which contains $POINT ID = 600001 and so on. But it does not contain names behind it as $POINT ID = 600001shri.

So I tried to take user defined input from inpt.txt , using it in input1.txt and taking this changes in output.txt...

I am almost done this with my program for $POINT ID = 600001Shri but I COULD NOT REPEAT IT FOR THE REST OF input1.txt. Simply I think I could not repeat it for other points.(Looping Problem).

I thought I can create the file with cout<<search and I am getting correct output as
$POINT ID = 600001Shri
$POINT ID = 600002Shri
$POINT ID = 600003Shri
$POINT ID = 600004Proactive
$POINT ID = 600005Proactive
$POINT ID = 600006Proactive

But when I am trying to use it in File Management section by find and replace things I think It is not repeating loop for other points 6000002, 6000003 and so on.

So there is problem with file management section.
I used pch.clear() and pch.(0,ios::beg) so that I think the pointer will automatically reset to initial position of input1.txt but I am not getting any change in output.

Please check my program .

Thank you in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.