Thread: Find replace strings in file in loop

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    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
    Last edited by somshridhar; 09-03-2011 at 11:41 PM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    Any reply for this thread please

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could start with cleaning up the indentation, so that people would want to read it. That's probably why it was ignored first time around.
    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");
    
      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;
    }
    Next, get rid of all those useless unused variables.
    Code:
    $ g++ -W -Wall -ansi -pedantic -O2 foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:22: warning: unused variable ‘input2’
    foo.cpp:22: warning: unused variable ‘name2’
    foo.cpp:29: warning: unused variable ‘k’
    foo.cpp:29: warning: unused variable ‘l’
    foo.cpp:29: warning: unused variable ‘m’
    foo.cpp:29: warning: unused variable ‘a’
    foo.cpp:29: warning: unused variable ‘b’
    foo.cpp:29: warning: unused variable ‘z’
    foo.cpp:29: warning: unused variable ‘r’
    foo.cpp:29: warning: unused variable ‘s’
    foo.cpp:29: warning: unused variable ‘d’
    foo.cpp:29: warning: unused variable ‘f’
    foo.cpp:30: warning: unused variable ‘e’
    foo.cpp:31: warning: unused variable ‘g’
    foo.cpp:31: warning: unused variable ‘h’
    foo.cpp:31: warning: unused variable ‘t’
    foo.cpp:32: warning: unused variable ‘u’
    foo.cpp:32: warning: unused variable ‘v’
    foo.cpp:32: warning: unused variable ‘w’
    foo.cpp:33: warning: unused variable ‘name’
    foo.cpp:33: warning: unused variable ‘input’
    foo.cpp:33: warning: unused variable ‘name1’
    I see there is an eof() in there, which is almost certainly wrong.
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    Once you reach the end of the file, you need to do something like fin.clear() to reset the eof() state, and then fin.seekg() to go back to the start of the file, if you want to read it again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Open File
    2. Read contents into memory
    3. Close file

    That is a good starting point. Now, I would drop C-style strings and use std::string here to making things simple. You need to be more specific about what your requirements are for this assignment, for instance:
    • Are you allowed to use the STL?
    • If so, what are your limitiations imposed by the class?
    • Do you know how to use std::vector?
    • Are you familiar with the algorithm lib?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find and replace a string in a file
    By salzouby in forum C Programming
    Replies: 13
    Last Post: 09-14-2010, 08:55 AM
  2. Using .find and .replace with strings
    By bleuz in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2010, 11:46 PM
  3. Strings Find and Replace
    By kakayoma in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2009, 06:18 PM
  4. Find and replace Strings.
    By shabbirhussain in forum C Programming
    Replies: 2
    Last Post: 08-01-2008, 10:48 PM
  5. Replies: 10
    Last Post: 06-10-2008, 02:17 AM