Replace string in C ++ file

I am trying to find a way to replace a line containing a line in a file with a new line.

If the line is not in the file, add it to the file.

Can someone give some sample code?

EDIT: is there anyway if the line I need to replace is at the end of the file?

+4
source share
4 answers

Although I understand that this is not the smartest way to do this, the following code reads demo.txt line by line and looks for the word cactus to replace it with oranges when writing the output to a secondary file called result.txt.

Donโ€™t worry, I saved some work for you. Read the comment:

#include <iostream> #include <fstream> #include <string> #include <algorithm> using namespace std; int main() { string search_string = "cactus"; string replace_string = "oranges"; string inbuf; fstream input_file("demo.txt", ios::in); ofstream output_file("result.txt"); while (!input_file.eof()) { getline(input_file, inbuf); int spot = inbuf.find(search_string); if(spot >= 0) { string tmpstring = inbuf.substr(0,spot); tmpstring += replace_string; tmpstring += inbuf.substr(spot+search_string.length(), inbuf.length()); inbuf = tmpstring; } output_file << inbuf << endl; } //TODO: delete demo.txt and rename result.txt to demo.txt // to achieve the REPLACE effect. } 
+2
source

To replace the last two lines in your file:

  • Use fopen() to open a file
  • Get current file position with fgetpos()
  • Always save the last two positions of a file and read line by line
  • When you have reached the end of the file: the bottom position is where you need to position with fseek()
  • Now you can fprintf() add your new lines to the file and then close it fclose() .
+1
source

As already mentioned, KarlPhilip code is a good starting point (thanks), but does not work correctly according to "> = 0". Compared to "0", it is known for MS CString types and in C #, etc., but it does not seem to work properly with STL. For instance. in VS 2010 (only in release) C ++ code did wrong with this comparison without causing an error!

Here's a fix to C ++ standards: if some wizards have improvements, submit them. I think this is a very useful and important question / snippet.

 #include <iostream> #include <fstream> #include <string> #include <algorithm> using namespace std; int main() { string search_string = "cactus"; string replace_string = "oranges"; string inbuf; // Todo: Check, if input_file exists before. // Todo: Try/catch or check, if you have write access to the output file. fstream input_file("demo.txt", ios::in); ofstream output_file("result.txt"); while (!input_file.eof()) { getline(input_file, inbuf); size_t foundpos = inbuf.find(search_string); if(foundpos != std::string::npos) { string tmpstring = inbuf.substr(0,spot); tmpstring += replace_string; tmpstring += inbuf.substr(spot+search_string.length(), inbuf.length()); inbuf = tmpstring; } output_file << inbuf << endl; } //TODO: delete demo.txt and rename result.txt to demo.txt // to achieve the REPLACE effect. } 

I used the following replacement part inside the "if" from another SO question (The code above replaces only the first occurrence):

 if(foundpos != std::string::npos) replaceAll(inbuf, search_string, replace_string); //http://stackoverflow.com/questions/3418231/c-replace-part-of-a-string-with-another-string // void replaceAll(std::string& str, const std::string& from, const std::string& to) { size_t start_pos = 0; while((start_pos = str.find(from, start_pos)) != std::string::npos) { size_t end_pos = start_pos + from.length(); str.replace(start_pos, end_pos, to); start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' } } 
+1
source

If you want to change the length (or delete) the line in the middle of the file, you will have to rewrite all subsequent lines.

You cannot just delete or paste characters into an existing file.

0
source

Source: https://habr.com/ru/post/1332890/


All Articles