Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. how to use QFile().flash?
Forum Updated to NodeBB v4.3 + New Features

how to use QFile().flash?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 585 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.
  • nishiokasN Offline
    nishiokasN Offline
    nishiokas
    wrote on last edited by
    #1

    We have this code.
    Please tell us these question.
    1.This code do "flash()" after "close()", is this collect?
    2.If "flash()" return false, this code can't break out of the infinite loop. Is it possible?
    if return false, it will return true in near future absolutely?

        QFile file(filename);
        if (file.open(QIODevice::WriteOnly|QIODevice::Append))
        {
            QTextStream out(&file);
            for(int i = 0; i < 128; i++) {
                out << log.sData[i];
                out << ",";
            }
            file.close();
            for( ; ; ) {
                if( file.flush() ) {
                    break;
                }
            }
        }
    
    

    Thanks.

    jsulmJ 1 Reply Last reply
    0
    • nishiokasN nishiokas

      We have this code.
      Please tell us these question.
      1.This code do "flash()" after "close()", is this collect?
      2.If "flash()" return false, this code can't break out of the infinite loop. Is it possible?
      if return false, it will return true in near future absolutely?

          QFile file(filename);
          if (file.open(QIODevice::WriteOnly|QIODevice::Append))
          {
              QTextStream out(&file);
              for(int i = 0; i < 128; i++) {
                  out << log.sData[i];
                  out << ",";
              }
              file.close();
              for( ; ; ) {
                  if( file.flush() ) {
                      break;
                  }
              }
          }
      
      

      Thanks.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @nishiokas

      1. Yes. Explained here: https://doc.qt.io/qt-5/qfiledevice.html#close
      2. You should have a second escape condition for that loop. I don't think it is guaranteed that flush() will return true.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      jeremy_kJ 1 Reply Last reply
      1
      • jsulmJ jsulm

        @nishiokas

        1. Yes. Explained here: https://doc.qt.io/qt-5/qfiledevice.html#close
        2. You should have a second escape condition for that loop. I don't think it is guaranteed that flush() will return true.
        jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        @jsulm I think there's some confusion here.

        Calling close() followed by flush(), or any file operation that expects an open file doesn't make sense. For better or worse, that resource is gone. The QIODevice::openMode() is set to make flush() a successful no-op.

        Call flush() on its own if the program benefits from increasing the chance that a user or file system sees the data before proceeding, or if knowing of a failure is useful. flush() may never succeed. The device could be full, or physically removed.

        QFile::close() calls flush() before closing the file. As noted in the documentation, it ignores a flush() failure. If you can't do anything anyway, this is the way to go.

        Getting back to the original 2 questions:

        1. No. Don't close(); ... flush();. Use flush();... close(), or just close().
        2. while (!flush()) on a closed file will not lead to an infinite loop, but is pointless. while (!flush()) before close() may lead to an infinite loop. Based on https://code.woboq.org/qt5/qtbase/src/corelib/io/qfsfileengine.cpp.html#472, if flush() returns false once, it will continue to do so until the file is closed.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        jsulmJ 1 Reply Last reply
        2
        • jeremy_kJ jeremy_k

          @jsulm I think there's some confusion here.

          Calling close() followed by flush(), or any file operation that expects an open file doesn't make sense. For better or worse, that resource is gone. The QIODevice::openMode() is set to make flush() a successful no-op.

          Call flush() on its own if the program benefits from increasing the chance that a user or file system sees the data before proceeding, or if knowing of a failure is useful. flush() may never succeed. The device could be full, or physically removed.

          QFile::close() calls flush() before closing the file. As noted in the documentation, it ignores a flush() failure. If you can't do anything anyway, this is the way to go.

          Getting back to the original 2 questions:

          1. No. Don't close(); ... flush();. Use flush();... close(), or just close().
          2. while (!flush()) on a closed file will not lead to an infinite loop, but is pointless. while (!flush()) before close() may lead to an infinite loop. Based on https://code.woboq.org/qt5/qtbase/src/corelib/io/qfsfileengine.cpp.html#472, if flush() returns false once, it will continue to do so until the file is closed.
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @jeremy_k said in how to use QFile().flash?:

          Calling close() followed by flush(), or any file operation that expects an open file doesn't make sense.

          You're right of course I did not notice that there was a close() before flush(). @nishiokas this indeed makes no sense.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

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