I need to write program that reads in string from input file and whenever it finds "foo" it should replace it with "xxx" and send corrected string to output file
my result is
axxxt barefoot buffoon
axxxt barexxxt buffoon
axxxt barexxxt bufxxxn
which is wrong
it should be
axxxt barexxxt bufxxxn
Your while condition is checking the state of the stream and it probably loops infinitely. You want to check that your done replacing instead. std::string::find returns std::string::npos if it did not find a match.