Thread: please help!...find and replace

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    there's a website you may have heard of... begins with a g?
    anyway if you type stuff in there it finds it for you! in fact if you type strcpy or memcpy the very first link is what you want.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Linux man...

    Why not the official Cppreference?
    http://www.cppreference.com/stdstring/index.html
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by maxorator
    Linux man...

    Why not the official Cppreference?
    http://www.cppreference.com/stdstring/index.html
    I'd pretty much prefer the real official reference for C.
    JTC1/SC22/WG14 - C
    Grab n1124.pdf.

    Or maybe see if you can find a C++ draft here:
    JTC1/SC22/WG21 - The C++ Standards Committee

    Or maybe http://dinkumware.com/
    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.*

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, 10:21 PM
  5. find and replace command?
    By p0wp0wpanda in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 11:42 PM