1

I want to edit a text file, but I'm stuck in finding the correct functions or methods to do so. So far I'm able to open a text file and look for a certain string, but I have no idea on how to move the cursor, add or replace information, steps 4 - 7 in my pseudocode shown below.

Can you provide some guidance? Which functions should I use (in case they already exist)? A sample 'easy' code would be appreciated as well.

Pseudocode:

1. Open file.
2. While not eof
3.    Read file until string "someString" is found.
4.    Position the cursor at the next line (to where the someString was found).
5.    If "someString" = A go to step 6. Else go to step 7. 
6.       Replace the information in whole line with "newString". Go to step 8.
7.       Add new information "newString_2", without deleting the existing.
8. Save and close the text file.

Thanks.

Francesco Boi
  • 5,497
  • 8
  • 54
  • 83
  • Files don't have "cursors" as such. Are you talking about writing a *visual* text editor, or are you talking about just reading text from a file and then writing out a new version of it? – crashmstr Jun 26 '14 at 15:41
  • 1
    You cannot replace a line directly in a file, files don't make such distinction. My suggestion is that you read the file and write the desired output to a new one. – Havenard Jun 26 '14 at 15:54
  • @Havenard this would be the easier solution, but you can "replace a line" by deleting content and inserting new content instead. –  Jun 26 '14 at 15:55
  • 1
    Yes, but it requires shifting the information after the space needed to fit the line in order to prevent data loss. A considerably complexier procedure. And you will have to repeat this process every time your search match a line. Not an efficient solution. – Havenard Jun 26 '14 at 15:58
  • @crashmstr I'm talking about writing a new version of it. Sorry, I didn't have other explanation (cursor) to describe the line or point that the program is currently reading. –  Jun 26 '14 at 15:59
  • @Havenard Are you suggesting to make a copy of the whole file? Because I may have text files as big as 400MB, and i would think that a copying process will take a lot of time... –  Jun 26 '14 at 16:18
  • @user3408085 Isn't that what `sed` and `awk` where made for? Or are you asking for academic reasons? – πάντα ῥεῖ Jun 26 '14 at 16:21
  • @πάντα ῥεῖ I didn't know about sed and awk until now. I must get literate about it. Thanks for your contribution. Oh, and programming is a hobby for me, neither work or academic reasons for it (yet). –  Jun 26 '14 at 16:40

2 Answers2

2

I would recommend to put the getline command into the while loop because then it won't stop only because of EOF but when getline is not able to read anymore. Like when the error bad occurs (which happens when someone deleted the file while your program was reading it).

It seems like you want to search inside a string, so "find" might be quite helpful.

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

int main (){
  std::fstream yourfile;
  std::string line, someString;

  yourfile.open("file.txt", ios::in | ios::app);  //The path to your file goes here

  if (yourfile.is_open()){  //You don't have to ask if the file is open but it's more secure
    while (getline(line)){
      if(line.find(someString) != string::npos){ //the find() documentation might be helpful if you don't understand
        if(someString == "A"){
          //code for replacing the line
        }
        else{
          yourfile << "newString_2" << endl;
        }
      } //end if
    } //end while
  } //end if
  else cerr << "Your file couldn't be opened";

  yourfile.close();
  return 0;
}

I can't tell you how to replace a single line in a text file but I hope you can work with that little I can give you.

Skardan
  • 21
  • 3
  • Yep, my answer was quick and incomplete as I considered the question trivial and heavily documented on the web. But I would also do that :) –  Jun 26 '14 at 18:04
  • 1
    I totally agree but I had some time to spare^^ – Skardan Jun 27 '14 at 06:57
0

This should be a good start:

// basic file operations
#include <string>
#include <fstream>

int main ()
{
  std::fstream myfile;
  std::string line;

  while (!myfile.eof())
  {
    std::getline(myfile,line); // Check getline() doc, you can retrieve a line before/after a given string etc.
    //if (line == something)
    //{
        // do stuff with line, like checking for content etc.
    //}
  }
  myfile.close();
  return 0;
}

More informations here

  • Avoid the following: `while (!myfile.eof())` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Mar 09 '20 at 18:43