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] Where I should write flush() function?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Where I should write flush() function?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 4.7k 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by
    #1

    Hi,

    Where I should write flush() function?

    Here:
    [CODE]
    out.flush();
    [/CODE]

    or here:
    [CODE]
    mFile.flush();
    [/CODE]

    [CODE]
    #include <QCoreApplication>
    #include <QFile>
    #include <QDebug>
    #include <QTextStream>

    void write(QString fileName) {
    QFile mFile(fileName);

    if (!mFile.open(QFile::WriteOnly | QFile::Text&#41;) {
        qDebug() << "Error: could not open file for writing";
        return;
    }
    
    QTextStream out(&mFile);
    out << "Hello world";
    
    // Here
    out.flush();
    
    // or here
    mFile.flush();
    
    mFile.close();
    

    }

    void read(QString fileName) {
    QFile mFile(fileName);

    if (!mFile.open(QFile::ReadOnly | QFile::Text&#41;) {
        qDebug() << "could not open file for reading";
        return;
    }
    
    QTextStream in(&mFile);
    QString mText = in.readAll();
    
    qDebug() << mText;
    
    mFile.close();
    

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    QString fileName = "C:/Test/text.txt";
    
    write(fileName);
    read(fileName);
    
    return a.exec(&#41;;
    

    }
    [/CODE]

    Thank you!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      in your case you can leave it out at all since it is called implicitly anyway by Qt.
      On destruction of QFile close() is called which also calls flush().

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • IamSumitI Offline
        IamSumitI Offline
        IamSumit
        wrote on last edited by
        #3

        Hello 8Observer8,
        You need to understand the difference of these 2s:
        void QTextStream::flush ()
        Flushes any buffered data waiting to be written to the device.
        If QTextStream operates on a string, this function does nothing.

        bool QFile::flush ()
        Flushes any buffered data to the file. Returns true if successful; otherwise returns false. [From Qt References]

        Be Cute

        1 Reply Last reply
        0
        • 8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by
          #4

          Thank you, guys!

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            If solved, place [SOLVED] in the title of your first post!

            Greetz, Jeroen

            1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by
              #6

              Sorry! Thank you :)

              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