Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QFile $ QStream What is that?
Forum Updated to NodeBB v4.3 + New Features

[Solved]QFile $ QStream What is that?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Polto
    wrote on last edited by
    #1

    hi , is that a bug or something i can't understand
    @#include <QtCore>

    int main()
    {
    QFile File("test.txt");
    File.open(QIODevice::ReadWrite | QIODevice::Text);

    QTextStream out(&File);
    out << "QTextStream ,  First Line \n";
    
    File.write("QFile , Second Line \n");
    
    File.close();
    

    }@
    the output file
    test.txt is
    QFile , Second Line
    QTextStream , First Line

    I have more bugs with QTextStream or we must use one method QFile or QTextStream ?
    I write this code under Mac OSX ,Not tested on windows

    Polto

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Well,

      if you mix up QFile write calls and QTextStream write calls, I think that you can encounter some strange output due to the fact that (I suppose) QTextStream "buffers" its data for a while before writing for real.

      So I suggest to write your file using "only" QTextStream or "only" QFile write calls.

      Tony.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        From "QTextStream Class Reference":http://doc.trolltech.com/4.7/qtextstream.html:

        bq. Internally, QTextStream uses a Unicode based buffer, ...

        bq. Since the text stream uses a buffer, you should not read from the stream using the implementation of a superclass. For instance, if you have a QFile and read from it directly using QFile::readLine() instead of using the stream, the text stream's internal position will be out of sync with the file's position.

        So the conclusion is, as mentioned by Antonio: you must not mix up using methods of QTextStream and QFile on the very same file.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lyuts
          wrote on last edited by
          #4
          1. As Antonio mentioned you should use only one approach and don't mix direct file writing and text stream writing.
          2. By declaring your text stream you say that everything will go to file, but not as soon as you use << operator. When something is outputted via << operator it sits in a intermediate buffer and waits for flushing. So, you need to call

          @out.flush();@

          and this will put data from buffer into a file that your stream is bound to.

          I'm a rebel in the S.D.G.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Polto
            wrote on last edited by
            #5

            @out.flush();@
            thanks I think i understand now

            Polto

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved