Thread: Find and replace

  1. #16
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
        string r_place;
        string word;
        
        ifstream f_in("example.txt");//open file to READ
        
        cout<<"Enter word you wish to replace"<<endl;
        cin>>word;
        cin.ignore();
        
        int size = word.length();//find the length of the word
        
        cout<<"Enter word you wish to replace it with"<<endl;
        getline(cin,r_place,'\n');
        
        cin.ignore();
        cout<<"\n\n";
        
        
        string search;
        ofstream f_out("update.txt");//open file to WRITE
    	
        while(getline(f_in, search, '\n'))//read in a line
    	{
    	    
    	   
        string::size_type pos = -1;
    	while( ( pos = search.find(word, pos+1) ) != string::npos )
    	{
    	    search.replace(pos,size,r_place);
    		//search.erase( (search.begin() + pos), (search.begin() + (pos + size)) );
    		//search.insert( pos, r_place );
    	}
    	f_out << search << endl;//Write to file
    	    
    		
        }
        f_in.close(); //close file (reading)
        f_out.close(); //close file (writing)
        cin.get();
        
        
    }
    You are almost there but the order of your program is slightly wrong. Also you need to open up a file to write to.
    The corrected file is named "update.txt"


    Code:
    example.txt
    ---------------
    Hello
           my
                name
                         is 
                            treenef
    Sample input:
    Code:
    Enter word you wish to replace
    treenef
    Enter word you wish to replace it with
    Sam Granger
    Code:
    update.txt
    -------------
    Hello
           my
                name
                         is 
                            Sam Granger

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Borland c++ builder 6 is popping this back at me when i try to run it.

    Build
    [Linker Error] Unresolved external '__InitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MTI.LIB|crtlvcl
    [Linker Error] Unresolved external '__ExitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MTI.LIB|crtlvcl

  3. #18
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    That's because you are not linking the vcl library, why are you using vcl anyways?
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #19
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I'm using Devshed ++ so I'm not sure what's going on.
    Someone else who is familiar with the borland compiler will have to take over.

    I can only make suggestions but I'm unsure if they will work.

    One thing I noticed was that in your previous code you had this:

    Code:
    #include <vcl.h>
    Maybe you need to include that in your code perhaps. And Possibly change ...

    Code:
    #include <cstring>   to  #include<string>
    assuming that worked for you last time?


  5. #20
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Thanks!! That made it work.

  6. #21
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Right, now I have to get this to work in a win32 c++ app. Next step hehe.

  7. #22
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    have to? So is this an assignment or job? Or is this something I can help you with; I like Win32 API programming.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  8. #23
    Registered User
    Join Date
    Sep 2005
    Posts
    35
    Quote Originally Posted by jmd15
    have to? So is this an assignment or job? Or is this something I can help you with; I like Win32 API programming.
    Its just something I'd like to learn how to do. All help si really more then welcome!!

    I learn by looking at sourcecode i guess

  9. #24
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well that code snippet that treenef found would look be something like this in Win32:
    Code:
    #include <windows.h>
    #include <fstream>
    #include <string>
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    char szClassName[ ] = "WindowsApp",word[256],r_place[256],*words;
    std::string sword,sr_place;
    HWND static1,static2,edit1,edit2,button;
    std::fstream f_in;
    std::ofstream f_out;
    int size;
    int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,int nFunsterStil){
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        if (!RegisterClassEx (&wincl))
            return 0;
        hwnd = CreateWindowEx (0,szClassName,"Windows App",WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT,CW_USEDEFAULT,500,250,HWND_DESKTOP,NULL,
               hThisInstance,NULL);
        ShowWindow (hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0)){
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
        switch (message){
            case WM_CREATE:
                static1=CreateWindowEx(0,"Static","Replace",WS_CHILD|WS_VISIBLE,20,
                    20,60,25,hwnd,(HMENU)50000,GetModuleHandle(NULL),NULL);
                edit1=CreateWindowEx(0,"Edit","",WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,80,
                    18,100,22,hwnd,(HMENU)50001,GetModuleHandle(NULL),NULL);
                static2=CreateWindowEx(0,"Static","with",WS_CHILD|WS_VISIBLE,190,
                    20,30,25,hwnd,(HMENU)50002,GetModuleHandle(NULL),NULL);
                edit2=CreateWindowEx(0,"Edit","",WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,225,
                    18,100,22,hwnd,(HMENU)50003,GetModuleHandle(NULL),NULL);
                button=CreateWindowEx(0,"Button","Replace",WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,335,
                    18,70,22,hwnd,(HMENU)50004,GetModuleHandle(NULL),NULL);
                SendMessage(edit1,EM_LIMITTEXT,256,0);
                SendMessage(edit2,EM_LIMITTEXT,256,0);
                break;
            case WM_COMMAND:
                switch(wParam){
                case 50004:
                    GetWindowText(edit1,word,256);
                    GetWindowText(edit2,r_place,256);
                    sword=word;
                    sr_place=r_place;
                    size=sword.length();
                    if(size==0){
                    MessageBox(hwnd,"You must specify the word to replace","Problem",MB_OK|MB_ICONINFORMATION);
                    }
                    else{
                    std::string search,add; 
                    f_in.open("update.txt");
                    while(std::getline(f_in,search)){
                    std::string::size_type pos = -1;
                    while((pos=search.find(sword,pos+1))!=std::string::npos){ 
                    search.replace(pos,size,sr_place);
                    }
                    add+=search+"\n";
                    } 
                    f_out.open("update.txt");
                    f_out<<add;
                    f_out.close();
                    f_in.clear();
                    f_in.close();
                    }
                }
                break;
            case WM_DESTROY:
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    It works for me...
    And it can be done like this too:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string>
    #include <algorithm>
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    char szClassName[ ] = "WindowsApp",word[256],r_place[256],*words;
    std::string sword,sr_place;
    HWND static1,static2,edit1,edit2,button;
    char happy[256];
    FILE * pFile;
    int size;
    int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,int nFunsterStil){
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        if (!RegisterClassEx (&wincl))
            return 0;
        hwnd = CreateWindowEx (0,szClassName,"Windows App",WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT,CW_USEDEFAULT,500,250,HWND_DESKTOP,NULL,
               hThisInstance,NULL);
        ShowWindow (hwnd, nFunsterStil);
        while (GetMessage (&messages, NULL, 0, 0)){
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
        switch (message){
            case WM_CREATE:
                static1=CreateWindowEx(0,"Static","Replace",WS_CHILD|WS_VISIBLE,20,
                    20,60,25,hwnd,(HMENU)50000,GetModuleHandle(NULL),NULL);
                edit1=CreateWindowEx(0,"Edit","",WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,80,
                    18,100,22,hwnd,(HMENU)50001,GetModuleHandle(NULL),NULL);
                static2=CreateWindowEx(0,"Static","with",WS_CHILD|WS_VISIBLE,190,
                    20,30,25,hwnd,(HMENU)50002,GetModuleHandle(NULL),NULL);
                edit2=CreateWindowEx(0,"Edit","",WS_BORDER|WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,225,
                    18,100,22,hwnd,(HMENU)50003,GetModuleHandle(NULL),NULL);
                button=CreateWindowEx(0,"Button","Replace",WS_BORDER|WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,335,
                    18,70,22,hwnd,(HMENU)50004,GetModuleHandle(NULL),NULL);
                SendMessage(edit1,EM_LIMITTEXT,256,0);
                SendMessage(edit2,EM_LIMITTEXT,256,0);
                break;
            case WM_COMMAND:
                switch(wParam){
                case 50004:
                    GetWindowText(edit1,word,256);
                    GetWindowText(edit2,r_place,256);
                    sword=word;
                    sr_place=r_place;
                    size=sword.length();
                    if(size==0){
                    MessageBox(hwnd,"You must specify the word to replace","Problem",MB_OK|MB_ICONINFORMATION);
                    }
                    else{
                    std::string search,add;
                    pFile = fopen ("update.txt" , "r");
                    while(fgets(happy,1500,pFile)){
                    search=happy;
                    std::string::size_type pos = -1;
                    while((pos=search.find(sword,pos+1))!=std::string::npos){ 
                    search.replace(pos,size,sr_place);
                    }
                    add+=search+"\n";
                    }
                    fclose(pFile);
                    pFile = fopen ("update.txt" , "w+");
                    *(std::copy(add.begin(), add.end() - add.begin() < 256 ? add.end() : add.begin() + 255, happy)) = 0;
                    fputs(happy,pFile);
                    fclose(pFile);
                    }
                }
                break;
            case WM_DESTROY:
                PostQuitMessage (0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
    which results a smaller executable...
    Last edited by maxorator; 11-03-2005 at 01:55 PM.

  10. #25
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Yeah, those would work too, but check in the Windows Programming board. Sam posted this problem there as well and I already made it for him, so he can learn from the code. I incorporated some things such as common dialogs(File Open, and File Save As), some basic Win32 GDI, Edit and Button windows, etc.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

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. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  5. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 10:21 PM