Results 1 to 4 of 4

Thread: QTextStream + QFile issue

  1. #1
    Join Date
    Aug 2007
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextStream + QFile issue

    Hi ,
    before sending this issue to Trolltech maybe some one of you, skilled people, can answer where I'm wrong:

    Qt Code:
    1. //main.cpp
    2.  
    3. include <QtCore>
    4. int main(int argc, char **argv)
    5. {
    6. QFile f("main.cpp");
    7. f.open(QIODevice::ReadOnly);
    8. QTextStream ts(&f);
    9. QString s = ts.readLine();
    10. qDebug("%s", s.toLatin1().data());
    11. f.flush();
    12. f.close();
    13. f.setFileName("pippo.txt");
    14. f.open(QIODevice::ReadOnly);
    15. ts.flush(); //does nothing
    16. ts.setDevice(&f); //AGAIN?
    17. s = ts.readLine();
    18. qDebug("This one will not work");
    19. qDebug("First line: %s", s.toLatin1().data());
    20. ts.seek(0);
    21. s = ts.readLine();
    22. qDebug("This one will work");
    23. qDebug("First line: %s", s.toLatin1().data());
    24.  
    25. return 0;
    26. }
    27.  
    28. //pippo.txt
    29. ** If you read this line the QTextStream class works **
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream + QFile issue

    What do u wants to do?
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTextStream + QFile issue

    QTextStream::flush() flushes write buffer so don't expect it to do anything on a read only buffer. But I think I can see your point. You're saying that QTextStream::setDevice() should reset text stream's internal position, right?
    J-P Nurmi

  4. #4
    Join Date
    Aug 2007
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextStream + QFile issue

    Ok seems that I do not explain the point, my fault.
    If you run the prog you will notice that the out put will be this:

    #include <QtCore>
    This one will not work
    First line: #include <QtCore>
    This one will work
    First line: ** If you read this line the QTextStream class works **

    Closing the first file and opening another one does not affect QTextStream buffer until seek(0) is called.
    If flush() is intended for write only, how could we delete the buffer when we open another file?
    the answer seems to be "call seek(0)";
    If you make a prog that ask for a file via QfileDialog, read the first line of the file using QTextStream:: readLine() and display it in a TextEdit. changing the file will not change the TextEdit contents because QTextStream::readLine() continue to read the same data.
    QTextStream::readAll() is not affected.
    Any comments are welcome.

Similar Threads

  1. QFile, QTextStream, seek()
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2006, 15:07
  2. Qfile and QTextStream
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2006, 10:43
  3. QSettings vs (QFile + Qtextstream)
    By nupul in forum Newbie
    Replies: 5
    Last Post: 10th April 2006, 07:26

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.