i want to replace a certain text in text file and i can't figure it out the code that need to perform this function.
example in my text file have
12345 hello
789 morning
1234 good

text i want to edit
morning
text to replace
night

result :
12345 hello
789 night
1234 good

this is my code

string temp;
ifstream seek1;
ofstream seek2;
seek1.open( "staff_info.txt");
seek2.open( "staff_info.txt", fstream::in | fstream::out | fstream::ate | ios::app);

                            cout<<"Enter the text that you want to edit\n";
                            getline(cin, st_txt1);
                            cout<<"Enter the text to replace\n";
                            getline(cin, st_txt2);

                                while (getline(seek1, temp))
                                    {
                                        if (temp.find(st_txt1)!=string::npos) 
                                        {
                                            temp.replace(temp.find(st_txt1),st_txt1.length(),st_txt2);
                                            seek2 << temp;
                                         }

                                }

Have a look at the string class: Click Here
Also, have a look at the algorithm class: Click Here
Use functions as replace/find.
Also, here are two examples of funtions:
removes the 1st occurence from the string:

void rem(string &initialString, string whatToRemove){
    size_t pos=initialString.find(whatToRemove);
    if (pos!=string::npos)
        initialString.erase(initialString.begin()+pos, initialString.begin()+pos+whatToRemove.length());
}

replaces 1st occurence from a string with another string:

void rep(string &initialString, string whatToReplace, string withWhat){
    size_t rpos=initialString.find(whatToReplace);
    if (rpos!=string::npos)
        initialString.replace(initialString.begin()+rpos, initialString.begin()+rpos+whatToReplace.length(), withWhat);
}

Play a bit with these examples, and see how they fit into your problem.

The only way to replace one string with another string in a text file is to completely rewrite the file. Open the original text file for reading, open a temp file for writing. In a loop, read each line of the original file and rewrite it to the temp file, except write when you get to the line that needs to be replaced write the replacement string instead of the string read from the file. After all is done, close both files, delete the original file, then rename the temp file to the name of the original file.

If the file is small enough you could simplify the above by reading all the lines into memory at once, replace the line you want, then rewrite all the lines back to the original file.

This is my delete function
example of
12345 hello
789 morning
1234 good

when i type morning
it will delete the lines until morning
which means result is
1234 good

cin.ignore();
                            string s_delete;
                            ofstream temp1;
                            temp1.open("temp.txt");
                            cout << "Which record do you want to remove? "<<endl;
                            getline(cin, s_delete);
                            while (getline(seek1,temp))
                                {
                                    if (temp.find(s_delete) != string::npos)
                                        {
                                             temp1 << temp << endl;
                                        }
                                 }
                                temp1.close();
                                seek1.close();
                                remove("staff_info.txt");
                                rename("temp.txt","staff_info.txt");
                                cout <<endl<<"Delete success.\n";

What's problem with my code? Any help?

if (temp.find(s_delete) != string::npos)

Should be ==, not !=. You want to write the string if the comparison is false.

I can replace the text already

the method i use is
open original
copy original to temp
after edit
save to temp to temp.txt
remove & rename.

but another problem is the output.

example
//My text file data
hello 12345
good morning

after replace (replace morning to night)
output file become
good night

hello 12345 is gone, i donno where i do the mistake that didn't copy it to temp file.

This is my edit code

                            cin.ignore();
                            ifstream seek1;
                            ofstream temp1; 
                            seek1.open("staff.txt");
                            temp1.open( "temp.txt");
                            cout<<"Enter the text that you want to edit\n";
                            getline(cin, st_txt1);
                            cout<<"Enter the text that you want to replace\n";
                            getline(cin, st_txt2);

                                while (getline(seek1, temp))
                                    {
                                        if (temp.find(st_txt1) != string::npos) 
                                        {
                                                      temp.replace(temp.find(st_txt1), st_txt1.length(), st_txt2);
                                            cout << temp << endl;
                                            temp1 << temp << endl;
                                         }

                                    }
                            cout<<endl<<"Edit success.\n";

Any1 can help me?

Move line 17 down after the } on line 18 and outside that if statement. The line needs to be written whether the replacedment occurs or not.

Thanks for help!!

why the remove and rename function cannot perform?
everytime remove the original (hello.txt), it still remain.
and temp file (hi.txt) cannot rename to original file (hello.txt)
this is the code

remove("hello.txt");
rename("hi.txt","hello.txt");

is there any wrong about it?

looks right, maybe you misspelled the file name(s)? Comment out the rename() function and check if the original file was removed after running your program.

why the remove and rename function cannot perform?
everytime remove the original (hello.txt), it still remain.

Probaly because you have not closed the files before attempting these operations.

#include <cstdio>
#include <fstream>
#include <string>
#include <iostream>

std::string replace( std::string line, const std::string& substr,
                                       const std::string& replace_with )
{
    std::string::size_type pos = 0 ;
    while( ( pos = line.find( substr, pos ) ) != std::string::npos )
    {
        line.replace( pos, substr.size(), replace_with ) ;
        pos += replace_with.size() ;
    }
    return line ;
}

int main()
{
    const char* const original_file_name = "whatever" ;
    std::string original_substr, replacement ;
    std::cin >> original_substr >> replacement ;

    // get a temporary file name
    char tmp_file_name[ L_tmpnam ] ;
    std::tmpnam(tmp_file_name) ;

    // create a temporary file with replaced text
    {
        std::ifstream original_file(original_file_name) ;
        std::ofstream temp_file(tmp_file_name) ;
        std::string line ;
        while( std::getline( original_file, line ) )
            temp_file << replace( line, original_substr, replacement ) << '\n' ;
    }

    // overwrite the original file with the temporary file
    {
        std::ifstream temp_file(tmp_file_name) ;
        std::ofstream original_file(original_file_name) ;
        original_file << temp_file.rdbuf() ;
    }

    // and delete the temporary file
    std::remove(tmp_file_name) ;
}

Here is my code, i donno where is the problem of remove and rename the file.

                             {
                            cin.ignore();
                            fstream seek1;
                            ofstream temp1; 
                            seek1.open("staff.txt");
                            temp1.open( "Stafftemp.txt");
                            cout<<"Enter the text that you want to edit\n";
                            getline(cin, st_txt1);
                            cout<<"Enter the text that you want to replace\n";
                            getline(cin, st_txt2);

                                while (getline(seek1, temp))
                                    {
                                        if (temp.find(st_txt1) != string::npos) 
                                        {
                                            temp.replace(temp.find(st_txt1), st_txt1.length(), st_txt2);
                                            cout<< temp << endl;
                                         }
                                        temp1 << temp << endl;
                                    }
                                cout<<endl<<"Edit success.\n\n";
                                temp1.close();
                                seek1.close();
                                remove("staff.txt");
                                rename("Stafftemp.txt", "staff.txt");

                        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.