Results 1 to 3 of 3

Thread: Update txt file after clearing line in Qt

  1. #1
    Join Date
    Dec 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Update txt file after clearing line in Qt

    I am trying to update the text file after I cleared a line in the file. When I print after the clear the line is actually cleared but when I reopen the txt file the line is still there.
    Here is the Code.

    Qt Code:
    1. void myAccounts::on_removeB_clicked()
    2. {
    3. myFile = new QFile("accountsDB.txt");
    4. if(!myFile->open(QIODevice::Text | QIODevice::ReadWrite))
    5. qDebug()<<"1!!"<<endl;
    6. else
    7. qDebug()<<"3!!"<<endl;
    8. stream= new QTextStream(myFile);
    9.  
    10. while(!stream->atEnd())
    11. {
    12. line=stream->readLine();
    13. list = line.split(",");
    14. qDebug()<<ui->tableWidget->currentItem()->text()<<endl;
    15. qDebug()<<list[0]<<endl;
    16. qDebug()<<line<<endl;
    17.  
    18. if(ui->tableWidget->currentItem()->text() == list[0] || ui->tableWidget->currentItem()->text() == list[1])
    19. {
    20. line.clear();
    21. qDebug()<<line<<endl;
    22. myFile->close();
    23. }
    24. }
    25. ui->tableWidget->removeRow(ui->tableWidget->currentRow());
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 20th December 2010 at 13:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Update txt file after clearing line in Qt

    I see no instruction to save the new content to the file.

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Update txt file after clearing line in Qt

    You will have to read the whole file and hold that data in a temp variable, clear the file content, remove the line from the content, and write back the content with the line cleared.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 2
    Last Post: 13th August 2010, 10:08
  2. Replies: 0
    Last Post: 21st July 2010, 10:32
  3. update .txt file
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2010, 00:44
  4. update the xml file from QT program
    By rk0747 in forum Newbie
    Replies: 5
    Last Post: 28th January 2010, 18:20
  5. How to Update in .ini file?
    By phillip_Qt in forum Qt Programming
    Replies: 1
    Last Post: 15th April 2008, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.