Delete a line from a file in qt?

456 Views Asked by At

How to delete two consecutive line using an iterator??

I am trying to create a form to save user and password using QT3.3. But i also want to modify and delete the user name and password.I am able to delete the user name, but i am not able to delete the immediate next line.The code i have been using is:

QStringList::Iterator it;
it = qFind(lines.begin(), lines.end(), str);
if(it != lines.end())
{
 lines.erase(it);
}  

Can any one suggest any way?

1

There are 1 best solutions below

5
On

QStringList::erase returns an iterator that references the element that was behind the element you just erased, so:

it = lines.erase(it);

// it now references the line after the erased one, so go again to delete that:
if(it != lines.end()) {
  lines.erase(it);
}

Also, obligatory: Is there a reason you use Qt 3.3? That's ancient by now.