Find Replace function in C++
Dear Sir,
I am working on a project in which I can replace strings but I can not repeat it for other strings. I am just posting my problem with you My input.txt Code:
Enter_the_number_of_names Code:
$POINT ID = 600001 Code:
#include<iostream> Code:
$POINT ID = 600001Shri BUT I want output.txt as Code:
$POINT ID = 600001Shri |
After capturing numeric input from the stream, undoubtedly you forgot/neglected to also capture the newline that is still sitting in the stream's queue. This will not affect a subsequent attempt to capture another numeric input, but it will affect your ability to capture a string.
My suggestion is that you never accept raw numeric input; always read input as a string. For situations where you require numeric input, convert the inputted string into the desired format, and then verify to ensure the input is valid. A very simple example: Code:
#include <iostream> P.S. Format your code using better indentation, and then maybe I will peruse it more carefully. |
FInd Replace function in C++
Dear Sir,
I am formatting my code again properly. Please check it. If I do not use File Management section from the program, i.e. if I remove File input/output section from the program , I got the correct output with cout<<search<<replace<<endl;. But as soon as I enter the File Section for find and replace, I am getting only one correct answer i.e. $POINT ID 6000001Shri. But for other numbers I won't get. inpt.txt 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"); ofstream pchout; pchout.open("output.txt"); struct NVH n[200]; long int i,j,c,p,x,y; char nam; 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; } My output is $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 check my program so that I can proceed further. Thank you in advance |
Quote:
|
Ok I am formatting code as follows
Code:
#include<iostream> |
Sigh... the point of using CODE tags is to preserve whatever indentation you may have used during the course of developing the software. Assuming that you have used some level of indentation, here's what your code would look like:
Code:
#include <iostream> And now, onto the issue of your application... the "input.txt" file that you provided earlier contains data that 1) seems incorrect, or 2) cannot be parsed using this code: Code:
file >> nam; |
Find Replace Function in C++
Dear Sir,
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again My input.txt is Enter_the_number_of_names 2 Enter_the_name Enter_the_range_of_points Shri 600001 600003 Proactive 600004 600006 My input1.txt is $POINT ID = 600001 I went to college I am a disco dancer $POINT ID = 600001 I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 My program is [CODE] #include<iostream> #include<string> #include<fstream> #include<iomanip> #include<sstream> #include <cassert> 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"); ofstream pchout; pchout.open("output.txt"); ofstream file1; file1.open("output1.txt"); struct NVH n[200]; long int i,j,c,p,x,y; char nam[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; string dot(":"); replace=dot+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; file1<<search<<replace<<endl; 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(); file1.close(); return 0; } [\CODE ] My output is $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 But I want output as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002:Shri I don't go to college. $POINT ID = 600002:Shri It is tedious program $POINT ID = 6000003:Shri Shridhar Ram Rahim $POINT ID = 6000003:Shri Ram Krishna Shiva Brhma $POINT ID = 6000004:Proacive Very Easy Program $POINT ID = 6000004:Proacive Finish the program $POINT ID = 6000005:Proacive Life is nice $POINT ID = 6000005:Proacive I am swiming $POINT ID = 6000006:Proacive Sorry this is the end of the program $POINT ID = 6000006:Proacive With my file1<<search<<replace<<endl; I am getting correct output1.txt as $POINT ID = 600001:Shri $POINT ID = 600002:Shri $POINT ID = 600003:Shri $POINT ID = 600004:Proactive $POINT ID = 600005:Proactive $POINT ID = 600006:Proactive Please check it from yor side. I just can reproduce correct output.txt as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri But for remaining part I could not reproduce it. Thank in advance |
Dear Sir,
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again My input.txt is Enter_the_number_of_names 2 Enter_the_name Enter_the_range_of_points Shri 600001 600003 Proactive 600004 600006 My input1.txt is $POINT ID = 600001 I went to college I am a disco dancer $POINT ID = 600001 I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 My program is [CODE] #include<iostream> #include<string> #include<fstream> #include<iomanip> #include<sstream> #include <cassert> 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"); ofstream pchout; pchout.open("output.txt"); ofstream file1; file1.open("output1.txt"); struct NVH n[200]; long int i,j,c,p,x,y; char nam[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; string dot(":"); replace=dot+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; file1<<search<<replace<<endl; 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(); file1.close(); return 0; } [\CODE ] My output is $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 But I want output as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002:Shri I don't go to college. $POINT ID = 600002:Shri It is tedious program $POINT ID = 6000003:Shri Shridhar Ram Rahim $POINT ID = 6000003:Shri Ram Krishna Shiva Brhma $POINT ID = 6000004:Proacive Very Easy Program $POINT ID = 6000004:Proacive Finish the program $POINT ID = 6000005:Proacive Life is nice $POINT ID = 6000005:Proacive I am swiming $POINT ID = 6000006:Proacive Sorry this is the end of the program $POINT ID = 6000006:Proacive With my file1<<search<<replace<<endl; I am getting correct output1.txt as $POINT ID = 600001:Shri $POINT ID = 600002:Shri $POINT ID = 600003:Shri $POINT ID = 600004:Proactive $POINT ID = 600005:Proactive $POINT ID = 600006:Proactive Please check it from yor side. I just can reproduce correct output.txt as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri But for remaining part I could not reproduce it. Thank in advance -------------------------------------------------------------------------------- |
Dear Sir,
Sorry I did not put char nam[300] that is why output is not generating. Now with that modification and one more output1.txt which explains that if I do not use File Section i.e. the section from while(!pch.eof()) then I am getting correct answer but I could not able to reproduce it the file section. I am giving my program again My input.txt is Enter_the_number_of_names 2 Enter_the_name Enter_the_range_of_points Shri 600001 600003 Proactive 600004 600006 My input1.txt is $POINT ID = 600001 I went to college I am a disco dancer $POINT ID = 600001 I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 My program is [CODE] Quote:
My output is $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002 I don't go to college. $POINT ID = 600002 It is tedious program $POINT ID = 6000003 Shridhar Ram Rahim $POINT ID = 6000003 Ram Krishna Shiva Brhma $POINT ID = 6000004 Very Easy Program $POINT ID = 6000004 Finish the program $POINT ID = 6000005 Life is nice $POINT ID = 6000005 I am swiming $POINT ID = 6000006 Sorry this is the end of the program $POINT ID = 6000006 But I want output as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri I like coconut $POINT ID = 600002:Shri I don't go to college. $POINT ID = 600002:Shri It is tedious program $POINT ID = 6000003:Shri Shridhar Ram Rahim $POINT ID = 6000003:Shri Ram Krishna Shiva Brhma $POINT ID = 6000004:Proacive Very Easy Program $POINT ID = 6000004:Proacive Finish the program $POINT ID = 6000005:Proacive Life is nice $POINT ID = 6000005:Proacive I am swiming $POINT ID = 6000006:Proacive Sorry this is the end of the program $POINT ID = 6000006:Proacive With my file1<<search<<replace<<endl; I am getting correct output1.txt as $POINT ID = 600001:Shri $POINT ID = 600002:Shri $POINT ID = 600003:Shri $POINT ID = 600004:Proactive $POINT ID = 600005:Proactive $POINT ID = 600006:Proactive Please check it from yor side. I just can reproduce correct output.txt as $POINT ID = 600001:Shri I went to college I am a disco dancer $POINT ID = 600001:Shri But for remaining part I could not reproduce it. Thank in advance |
The logic of your program is wrong.
You have included the processing of the entire input1.txt file within the loop over point numbers within the loop over names. Once you have read the entire input1.txt file once and written the entire output, pch.eof() will be true for the remainder of the program, so there will be nothing to process for later point number or names. The correct logic for the program is to process all of input.txt before you process any of input1.txt. After all of input.txt has been read into internal data structures, you need a loop through all the lines of input1.txt. Within each iteration of that loop, you must find the right part of the data (read earlier from input.txt) to make the correct change (if any) to that line before writing it out. |
Find Replace function in C++
Dear Sir,
I tried with your suggesstion with little modifications as follows but I can get output in output1.txt correctly. But as I want to do the same thing with file section I could not. Quote:
My output is output1.txt as $POINT ID = 600001:Shri $POINT ID = 600002:Shri $POINT ID = 600003:Shri $POINT ID = 600004:Proactive $POINT ID = 600005:Proactive $POINT ID = 600006:Proactive Please suggest me proper way Thank you |
Can anybody help me to resolve my problem.
Just little modification required. Thank you shridhar |
Quote:
It certainly doesn't look like you tried my suggestion: Quote:
|
Dear Sir,
I tried with your suggestion. I read input.txt separately. I obtained the output.txt with my requirements. But I could not use the same thing in File Management section to find and replace. My question is How can I find strigs in input1.txt which are same as that in output.txt and replace them at the same location with that string and Name required and create output1.txt with the required chnages. that means to find $POINT ID = 600001 from input1.txt and replace it with $POINT ID = 600001:shri which I want to take from output.txt. Quote:
|
Quote:
If you fixed that, I think this "corrected" program is roughly what you were trying to do. I do not intend this code to be an example of good coding nor anything like the way I might code a similar project. I attempted to get a basically correct program only by rearranging lines of code that you had written. After reading all of input.txt into data structures, it processes input1.txt one line at a time. For each line of input1.txt it looks at all the data that was read from input.txt and uses whatever part of that input.txt data applies to that line of input1.txt. Code:
#include<iostream> |
All times are GMT -5. The time now is 07:53 PM. |