My goal is to open a .txt file and replace words in it then overwrite it. My goal is half way done. It couts it perfectly, but I am having a hard time overwriting the whole text file exactly how it couts it. I want the text to be exactly how it is displayed. In this example, I want to replace "ABC" with "DEF".
This is so-called for-if antipattern.
You do not need for loop here. Nor you need to output line right after replacing: you will output it again later.
replace it with:
1 2 3 4
std::string::size_type n;
while((n = Y.find("ABC")) != std::string::npos) { //In case if there more than one ABC in line
Y.replace(n, 3, "DEF");
}
Thank you both they worked great and I have used both of your suggestions together. So just for future reference, the way that I was doing it was not possible?