how to use QFile().flash?
-
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.
-
- Yes. Explained here: https://doc.qt.io/qt-5/qfiledevice.html#close
- You should have a second escape condition for that loop. I don't think it is guaranteed that flush() will return true.
-
@jsulm I think there's some confusion here.
Calling
close()
followed byflush()
, or any file operation that expects an open file doesn't make sense. For better or worse, that resource is gone. TheQIODevice::openMode()
is set to makeflush()
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()
callsflush()
before closing the file. As noted in the documentation, it ignores aflush()
failure. If you can't do anything anyway, this is the way to go.Getting back to the original 2 questions:
- No. Don't
close(); ... flush();
. Useflush();... close()
, or justclose()
. 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.
- No. Don't
-
@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.