Question

Hey I'm trying to mess around with Qt and for some reason the following code will create the desired text file, but never writes anything to it. Am I doing something wrong? I believe I've copied the example in the documentation pretty accurately.

qDebug() << output

works as expected, but even though the file is created, nothing is ever written to it.

#include <QCoreApplication>
#include <QtDebug>
#include <QString>
#include <QDateTime>
#include <QTextStream>

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

QString filename = "";
filename.append(QString::number(QDateTime::currentMSecsSinceEpoch()));
filename.append(".txt");

QFile file(filename);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);

QString output = "TEST";

qDebug() << output;
out << output;
return a.exec();
}
Was it helpful?

Solution

The data does not get written to disk immediately: It sits in a buffer until it's flushed.

Close the file after you've finished writing.

(In my experience, the file is closed anyway when you quit the program, but it's good practice to do this explicitly)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top