[SOLVED] Where I should write flush() function?
-
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)) { 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)) { 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();
}
[/CODE]Thank you!
-
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(). -
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] -
Thank you, guys!
-
If solved, place [SOLVED] in the title of your first post!
-
Sorry! Thank you :)