Editing a text file

12,821

Solution 1

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

Solution 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.

Share:
12,821

Related videos on Youtube

Francesco Boi
Author by

Francesco Boi

Interested in programming, electronic, math, physics and technology. In my free-time I like playing sports, going to the sea, watching movies and reading.

Updated on June 04, 2022

Comments

  • Francesco Boi
    Francesco Boi almost 2 years

    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.

    • crashmstr
      crashmstr almost 10 years
      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?
    • Havenard
      Havenard almost 10 years
      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.
    • Admin
      Admin almost 10 years
      @Havenard this would be the easier solution, but you can "replace a line" by deleting content and inserting new content instead.
    • Havenard
      Havenard almost 10 years
      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.
    • Admin
      Admin almost 10 years
      @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.
    • Admin
      Admin almost 10 years
      @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...
    • πάντα ῥεῖ
      πάντα ῥεῖ almost 10 years
      @user3408085 Isn't that what sed and awk where made for? Or are you asking for academic reasons?
    • Admin
      Admin almost 10 years
      @πάντα ῥεῖ 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).
  • Admin
    Admin almost 10 years
    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 :)
  • Skardan
    Skardan almost 10 years
    I totally agree but I had some time to spare^^
  • drescherjm
    drescherjm about 4 years