LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Find Replace function in C++ (https://www.linuxquestions.org/questions/programming-9/find-replace-function-in-c-904129/)

somshridhar 09-20-2011 11:58 PM

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
2
Enter_the_name
Enter_the_range_of_points
Shri
600001
600003
Proactive
600004
600006

My input1.txt
Code:

$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 code 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;   
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

Code:

$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

Code:

$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 I could not repeat the loop in file for other similar strings.

dwhitney67 09-21-2011 06:14 AM

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>
#include <sstream>
#include <string>

int main()
{
  int        age;
  std::string firstName;
  std::string genericInput;

  // prompt and get input
  std::cout << "Enter your age: ";
  std::getline(std::cin, genericInput);

  // convert input to number and validate
  std::stringstream ss(genericInput);
  ss >> age;
  if (ss.fail())
  {
      std::cerr << "\nFailed to capture number." << std::endl;
      return -1;
  }

  // prompt and get string input
  std::cout << "Enter your first name: ";
  std::getline(std::cin, firstName);

  if (std::cin.fail())
  {
      std::cerr << "\nFailed to capture first name." << std::endl;
      return -1;
  }


  std::cout << "Hello " << firstName << "!  So you are " << age << " years old?" << std::endl;
}


P.S. Format your code using better indentation, and then maybe I will peruse it more carefully.

somshridhar 09-23-2011 10:54 PM

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

dwhitney67 09-24-2011 04:16 AM

Quote:

Originally Posted by somshridhar (Post 4480698)
Dear Sir,

I am formatting my code again properly.

Sorry, I meant to indicate that the code should be formatted using CODE tags. Something like [CODE] your code [/CODE ] (without the last white-space).

somshridhar 09-24-2011 11:48 PM

Ok I am formatting code as follows

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");

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


dwhitney67 09-25-2011 09:50 AM

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>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cassert>  // ADDED

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

If you examine the code above, you will note that I had to include <cassert> so as to include the function prototype for assert(), which you use in your program.

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;
  file >> c;
  file >> nam;
  file >> nam;

Can you please clarify what your "input.txt" file is supposed to contain, and if what you posted earlier is correct, then please explain how do you expect the code above to function properly? Your OP indicated that your program was generating output, albeit incorrectly. Based on the code you posted, my assertion is that no output is being produced.

somshridhar 09-27-2011 09:47 AM

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

somshridhar 10-02-2011 11:55 AM

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

somshridhar 10-19-2011 01:12 PM

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:

#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

johnsfine 10-19-2011 01:30 PM

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.

somshridhar 11-11-2011 09:29 AM

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:

#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;
string search;
string replace;
file>>nam;
file>>c;
file>>nam;
file>>nam;
for(i=0;i<c;i++)
{
file>>n[i].name;
string str("$POINT ID = ");

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();

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

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

somshridhar 12-21-2011 11:37 PM

Can anybody help me to resolve my problem.
Just little modification required.

Thank you
shridhar

johnsfine 12-22-2011 03:00 PM

Quote:

Originally Posted by somshridhar (Post 4521571)
I tried with your suggesstion with little modifications as follows

Whose suggestion did you try?

It certainly doesn't look like you tried my suggestion:
Quote:

The correct logic for the program is to process all of input.txt before you process any of input1.txt.
You also didn't follow dwhitney67's suggestion about CODE tags, so this thread is very hard to read. That discourages most experts from helping you.

somshridhar 12-23-2011 09:46 AM

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:

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

ofstream file1;
file1.open("output1.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;
char nam[300],name[300],input[300],name1[300];
string line;
string replace;
string search;

file>>nam;

file>>c;
file>>nam;
file>>nam;

for(i=0;i<c;i++)
{
file>>n[i].name;
for(p=0;p<2;p++)
{
file>>n[i].points[p];
}
}
for(i=0;i<c;i++)
{
file>>n[i].name;
string str("$POINT ID = ");
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;
//cout<<j;
tc<<j;

string integer=tc.str();

search=str+integer;
file1<<search<<endl;
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();

pchout.close();
file1.close();
return 0;

}

johnsfine 12-23-2011 03:35 PM

Quote:

Originally Posted by somshridhar (Post 4502729)
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

Do you still have the extra digit (one more 0 in the middle) of 6000003- 6000006 that you don't have in 600001-600002?

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

    ofstream file1;
    file1.open("output1.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;
    char nam[300],name[300],input[300],name1[300];
    string line;
    string replace;
    string search;

    file>>nam;

    file>>c;
    file>>nam;
    file>>nam;

    for(i=0;i<c;i++)
    {
        file>>n[i].name;
        for(p=0;p<2;p++)
        {
            file>>n[i].points[p];
        }
    }

    while(!pch.eof())
    {
        getline(pch,line);
        for(i=0;i<c;i++)
        {
            string str("$POINT ID = ");
            string dot(":");
            replace=dot+n[i].name;
            x=n[i].points[0];
            y=n[i].points[1];

            for( j=x;j<=y;j++)
            {
                std::stringstream tc;
                //cout<<j;
                tc<<j;

                string integer=tc.str();
                search=str+integer;
                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();

    pchout.close();
    file1.close();
    return 0;
}



All times are GMT -5. The time now is 07:53 PM.