0

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?

Marek R
  • 32,568
  • 6
  • 55
  • 140
user3639779
  • 79
  • 1
  • 9
  • 3
    Current Qt is Qt5.4; you should not use the *ancient* Qt3.3 in 2015. And you should better read all the file in memory, then write a new one.... Or consider [Sqlite](http://sqlite.org/) or [gdbm](http://www.gnu.org/software/gdbm/) – Basile Starynkevitch Jan 05 '15 at 12:36
  • My work is based on RHEL 4 and the OS has qt 3.3 along with it so only i hv been using it. – user3639779 Jan 05 '15 at 12:45
  • Sorry, you can always compile Qt5.4 from its source code and install it. BTW, I am not sure that basing your work on RHEL4 (published in 2005) is sensible in 2015 (ten years later). – Basile Starynkevitch Jan 05 '15 at 12:50
  • 1
    @user3639779 If you're using RHEL4 and Qt3.3 it seems weird that you would post your questions on StackOverflow instead of dialing up your local BBS and posting there. – Jonathan Mee Jan 05 '15 at 12:51

1 Answers1

2

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.

Wintermute
  • 42,983
  • 5
  • 77
  • 80
  • i have been trying this but it gives segmentation fault on executing – user3639779 Jan 05 '15 at 12:41
  • My work is based on RHEL 4 and the OS has qt 3.3 along with it so only i hv been using it. – user3639779 Jan 05 '15 at 12:44
  • @user3639779: basing *current* work on something released ten years ago is very probably a *huge* mistake. In what industry are your working? – Basile Starynkevitch Jan 05 '15 at 12:57
  • But working on embedded systems does not justify using ancient software to leverage your developments. – Basile Starynkevitch Jan 05 '15 at 14:01
  • I have a sneaking suspicion that the segfault happens later in the program. What does gdb say? If it really crashes right there, the `QStringList` object has been smashed before this, but I have difficulty imagining a way in which it could have been smashed that would "survive" one `erase` but not two. – Wintermute Jan 05 '15 at 14:02