Results 1 to 7 of 7

Thread: QTextStream appends when run more than once

  1. #1
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextStream appends when run more than once

    Qt Code:
    1. QTextStream stream(&serverList);
    2. stream << document.toString();
    3. serverList.close();
    To copy to clipboard, switch view to plain text mode 

    how can i get it to overwrite the file and not append to it

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream appends when run more than once

    Don't open serverList in append mode.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream appends when run more than once

    how would i open it in overwrite ?

    << appends

    whats the alternative ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextStream appends when run more than once

    Rewind the device before you write to it again or reopen the device.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream appends when run more than once

    like seek(0)

  6. #6
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream appends when run more than once

    I've tried serverList.seek(0) but with no success i also tried resize but that failed as well

    Qt Code:
    1. QFile serverList("/stuff/Servers.xml");
    2. if(!serverList.open(QIODevice::WriteOnly | QIODevice::Text))
    3. {
    4. qDebug() << "Failed to open file for write";
    5. }
    6. else
    7. {
    8. serverList.seek(0);
    9. QTextStream stream(&serverList);
    10. stream << document.toString();
    11. serverList.close();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Prophet; 21st June 2012 at 17:09.

  7. #7
    Join Date
    Sep 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream appends when run more than once

    ok so apparently its not the file thats the problem, its document.toString() that once run again is appended to document.. i have tried to document.clear() after the stream << document.toString() but that still does not solve my issue any thoughts ?

Similar Threads

  1. QTextStream
    By lahmitia in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2011, 21:50
  2. QTextStream Problem
    By lahmitia in forum Newbie
    Replies: 3
    Last Post: 8th March 2011, 20:16
  3. QTextStream under Mac OS X
    By hunsrus in forum Qt Programming
    Replies: 0
    Last Post: 14th September 2009, 09:49
  4. QTextStream
    By WXNSNW in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 16:43
  5. QTextStream help pls
    By munna in forum Newbie
    Replies: 5
    Last Post: 16th May 2006, 18:45

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.