Empty file in Qt

I have a file called 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 to check if a file is empty or not in Qt?

+4
source share
2 answers

add the add flag and check the pointer insert:

 newFile.open( QIODevice::WriteOnly|QIODevice::Append ); if (newFile.pos() == 0) { // is empty } else { // some data inside } 

disclaimer: unverified code, now I will take the time to try ...

Edit: Checked seems to work well ...

+2
source

Check file size before opening newFile.size ()

+7
source

Source: https://habr.com/ru/post/1433198/


All Articles