Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QFile $ QStream What is that?
Qt 6.11 is out! See what's new in the release blog

[Solved]QFile $ QStream What is that?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 5.3k 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.
  • ? Offline
    ? Offline
    A Former User
    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

    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
      • ? Offline
        ? Offline
        A Former User
        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.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          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.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            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
            • Unsolved