please help!...find and replace

This is a discussion on please help!...find and replace within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm doing a basic course at uni and i have an assignment where i have to prompt the user ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    please help!...find and replace

    Hi!
    I'm doing a basic course at uni and i have an assignment where i have to prompt the user for a file name; find if the file exists, and then propt the user for a word to find in the file, and then a word to replace in the file. I then have to replace the word and output this to a new file.
    This is what i have done so far, but im unsure of how to do the find and replace stuff!
    All help would be very much appreciated; here's what ive done so far..also i cant use <string> cos i havent covered in class, i can only use <cstring>.

    Here's my code:

    Code:
    #include<iostream>
    #include<fstream>
    #include<cstring>
    using namespace std;
    
    int main()
    
    {
    
               char file[50];
               char string_find [50];
               char string_replace[50];
              
               cout << "Enter an input file: " << endl;
               cin >> file;
    
               ifstream infile;
               infile.open(file);
    
               if(infile.fail())
                     cout << "File could not be opened" << endl;
               else 
                {
                      cout << "Enter a string to find: " << endl;
                      cin >> string_find;
    
                      cout << "Enter a string to replace with: " << andl;
                      cin >> string_replace;
    
                      ofstream outfile;
                      outfile.open(strcat(file,".out"));
                      outfile << "test";
                   }
                   
                   return 0;
    }
    Your help is very much appreciated! Thanks in advamce, amy

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You'd be doing C++, however, to locate one string within another, you'll need to compare one char at a time, looping through the "haystack" looking for the "needle". There are, of course, functions that will do this for you, but that would negate the purpose of this exercise.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    please help!...find and replace

    Hi!
    I'm doing a basic course at uni and i have an assignment where i have to prompt the user for a file name; find if the file exists, and then propt the user for a word to find in the file, and then a word to replace in the file. I then have to replace the word and output this to a new file.
    This is what i have done so far, but im unsure of how to do the find and replace stuff!
    All help would be very much appreciated; here's what ive done so far..also i cant use <string> cos i havent covered in class, i can only use <cstring>.

    Here's my code:


    Code:
    #include<iostream>
    #include<fstream>
    #include<cstring>
    using namespace std;
    
    int main()
    
    {
    
               char file[50];
               char string_find [50];
               char string_replace[50];
              
               cout << "Enter an input file: " << endl;
               cin >> file;
    
               ifstream infile;
               infile.open(file);
    
               if(infile.fail())
                     cout << "File could not be opened" << endl;
               else 
                {
                      cout << "Enter a string to find: " << endl;
                      cin >> string_find;
    
                      cout << "Enter a string to replace with: " << andl;
                      cin >> string_replace;
    
                      ofstream outfile;
                      outfile.open(strcat(file,".out"));
                      outfile << "test";
                   }
                   
                   return 0;
    }
    Your help is very much appreciated! Thanks in advance, amy

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    oh ok. How would i go about doing that?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    yes, ive been reading about those functions but im not allowed to use them! Ive been reading some other posts that have a similar sort of thing but they all use <string> and im not sure how to use it doing <cstring>. How would i search through?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    <<Sorry about any confusion getting rid of the double post to both the C and C++ boards, merging replies (in the C thread), and moving this to a single thread on the C++ board.>>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    do i do some sort of loop or if? I really have no idea!! and i think it shows!!

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are you guaranteed that the replacement text will be no bigger than the find text? If not, it would seem that you may need to venture towards dynamic allocation. Well, unless you've got a "large enough" buffer to contain any possible text after replacement. Is it safe to make that assumption?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    no, the word i have to find is 'be' and the word i have to replace is 'there'. So they are not the same size.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So... are you familiar with dynamic allocation? Or do you have the "large enough" assumption?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    i have not heard of that...so iguess i would have to go with the 'large enough' assumption...what ever that is!!

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    please dont abandon me now! How do i do the find and replace stuff?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    *sigh* I love it when students are taught how to overflow buffers.

    Find text with strstr.
    Copy text with strcpy or memcpy.
    Move the remaining text with memmove.
    Lather, rinse, repeat (if necessary).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    ok...so how do i code that

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Look up the functions in the help/man pages. Make an attempt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
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. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 07:28 AM
  3. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  4. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 09:21 PM
  5. find and replace command?
    By p0wp0wpanda in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 11:42 PM

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