performing a search-and-replace operation on a file

This is a discussion on performing a search-and-replace operation on a file within the C++ Programming forums, part of the General Programming Boards category; I'm having a bit of trouble (surprise surprise). I'm trying to write a program that will prompt the user for ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    performing a search-and-replace operation on a file

    I'm having a bit of trouble (surprise surprise). I'm trying to write a program that will prompt the user for the word to search for and the word to use as a replacement. The program should then replace all of the instances of the target word with the replacement word.

    heres a sample of the input file:

    color.txt
    Code:
    red
    yellow
    orange
    purple
    green
    yellow
    green
    purple
    white
    purple
    A sample run of the program:

    Please input a word to search and a new word to replace
    purple
    blue


    The file will then update to:

    color.txt
    Code:
    red
    yellow
    orange
    blue
    green
    yellow
    green
    blue
    white
    blue

    I tried writting a program that loaded the file into an array and then compare each array value to the words searched...if they matched then it replaced it...if not it went to the next one...

    My question is... was that the right logic or was I trying to do something that was impossible. If I'm wrong...what should I do to make it right

    (*note* no code is needed....just gentle nudges toward an answer)

    I'll post my code if needed/wanted

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    3
    I saw the FAQ and saw that it is possible to compare strings using the IF statement

    this is the code I have

    Code:
        int w;
        string ifile[inf];
        char old[10],neww[10],temp[10];
    
        cout<<"Please input a word to search and a new word to replace"<<endl;
        cin>>old;
        cin>>neww;
    
        for (w=0;w<inf;w++)
        {
            temp=ifile[w];
            if (strcmp(temp,old) == 0)
                 {
                      ifile[w]==neww;
                 }
                 
        }
    am I doing this wrong? :-/

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Using C++ string objects, you can simply say:

    Code:
    if( temp == old )
    [edit]That is if both temp and old are strings. [/edit]
    Last edited by hk_mp5kpdw; 05-11-2004 at 12:02 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 11:08 AM
  2. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 11:33 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. open file, search of word, replace word with another
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-05-2002, 02:16 PM
  5. Search and replace coding....
    By bishop_74 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2001, 03:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21