Change
mFile.open(QFile::ReadWrite);
to
mFile.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text);
The QIODevice vs QFile distinction isn't necessary, but I personally favor using the base class. The Truncate
flag will overwrite (i.e., delete) an existing file.
Alternatively, you can follow the other suggestion and open your text file directly using one of QTextStream's constructors. The same QIODevice::OpenMode
conventions apply. This only works if mFile
is a FILE
object and not a QFile
, which isn't the case in your example.
A couple additional notes for beginners.
Related Note 1
You didn't ask about this, but I also added the QIODevice::Text
flag to ensure that newline characters get translated to/from the local encoding (plain \n
vs. \r\n
) when you use endl
.
Related Note 2
Using QTextStream::endl will automatically call flush()
each time. If your loop is large, use "\n"
instead to prevent a slowdown unless you actually need to flush every line.
QFile::close() also calls flush()
, which makes your mFile.flush()
at the end redundant.