I have a file named DBFile. I am using the following code:
QString DBfile ="C:/Users/E543925/Desktop/VikuTB.xml";
QFile newFile(DBfile);
newFile.open( QIODevice::WriteOnly);
Now I want to write something inside the file if it is empty. How can I check whether a file is empty or not in Qt?
add the append flag and check the insertion pointer
:
newFile.open( QIODevice::WriteOnly|QIODevice::Append );
if (newFile.pos() == 0) {
// is empty
} else {
// some data inside
}
disclaimer: untested code, now i'll take the time to try it...
edit: tested, seems to work well...
Check file size before open by newFile.size()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With