0

I have been attempting to make my own version of this example:

Delete a specific line from a file

But I cannot get mine to work correctly. My current goal for the program is to delete the string and its information (which is the string below the first string), which it does; however, it also adds an additional space to the first line so that it looks like this:

(First Line)


(Second Line) This should be the first line.

Here is the code:

infile = ifstream;
outfile = ofstream;
cout<< "What string would you like to delete";
cin>>delstr;
infile.clear();
infile.seekg(0, ios::beg);

ofstream tempfile;
tempfile.open("temp.txt",std::ios::app);

while(delreset == true){

    if(delstr == fLine){
        getline(infile, fLine);

        cout<<"String deleted.\n";
        delreset = false;

        while(fLine != nothing){
            getline(infile, fLine);
            tempfile<<fLine<<"\n";
        }

        tempfile.close();
        infile.close();
        outfile.close();
        remove("example.txt");
        rename("temp.txt","example.txt");

    }else{

        tempfile<<fLine<<endl;
        getline(infile, fLine);

    }
    outfile.flush();
    delreset = true;
}

I deleted what I could to make it an abridged version of the actual program, hopefully I didn't edit anything so that it doesn't make sense.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
bill
  • 51
  • 8
  • 1
    A nicely-formatted code sample that compiles will always get you farther on this site. There may be a bug in what you've posted, but I'm having trouble both reading it and guessing what code is missing. – Drew Dormann Jan 14 '15 at 18:54
  • `delreset == true` is a condition which is true only when delreset itself is also true. So you could just write `while(delreset)` – MSalters Jan 14 '15 at 19:47
  • The loop is setting `delreset` to true on every iteration, and there is no error handing on the input file reading, so the loop will never stop, even after the desired line has been found. – Remy Lebeau Jan 14 '15 at 19:50

2 Answers2

0

What with a simpler version:

...  // prepare everything as before
while(getline(infile, fLine)) {
    if(delstr == fLine) {   // if line found do nothing
        cout<<"String deleted.\n";
        getline(infile, fLine);  // EDIT: and read and ignore the following line 
    }
    else 
        tempfile<<fLine<<"\n";  // else copy it 
}
...  // here infile was read and tempfile contains the filtered output 

With this approach you could even write directly to the outfile.

By the way cin>>delstr; only takes a word. It stops at the first blank and ignores trailing blanks. You could use getline(cin, delstr); instead.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • The OP's requirement is to remove the specified line **AND** the line that follows it. This code does not do the latter. – Remy Lebeau Jan 14 '15 at 19:48
  • The test data gave impression of two numbered lines. So I misunderstood *"string below the first string"* as refering to the example. I've added and EDIT line, to remove the line that follows as well. – Christophe Jan 14 '15 at 22:31
0

Try something more like this instead:

cout << "What string would you like to delete";
getline(cin, delstr);
bool deleted = false;

infile.clear();
infile.seekg(0, ios::beg);

ofstream tempfile;
tempfile.open("temp.txt", std::ios::app);

while (getline(infile, fLine))
{
    if ((!deleted) && (fLine == delstr))
    {
        getline(infile, fLine);
        cout << "String deleted." << endl;
        deleted = true;
    }
    else
        tempfile << fLine << endl;
}

tempfile.close();
infile.close();
outfile.close();

if (deleted)
{
    remove("example.txt");
    rename("temp.txt", "example.txt");
}
else
    remove("temp.txt");
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770