I have a graphical (Qt, C++) application that stores its state in a data file and updates that file every 60 seconds.
I wrote a function to prevent data loss when the application or the OS is interrupted during writing.
QString updateFileSafely( const QString & origFilePath, const QByteArray & newContent )
{
QString newFilePath = origFilePath+".new";
QFile newFile( newFilePath );
if (!newFile.open( QIODevice::WriteOnly ))
{
return "Could not open file "%newFilePath%" for writing: "%newFile.errorString();
}
newFile.write( newContent );
if (newFile.error() != QFile::NoError)
{
return "Could not write to file "%newFilePath%": "%newFile.errorString();
}
newFile.close();
QFile oldFile( origFilePath );
if (oldFile.exists())
{
if (!oldFile.remove())
{
return "Could not delete the previous file "%origFilePath%": "%oldFile.errorString();
}
}
if (!newFile.rename( origFilePath ))
{
return "Could not rename the new file "%newFilePath%" back to "%origFilePath%": "%newFile.errorString();
}
return "";
}
This code should only remove the old file after the new file is successfully written and closed.
Yet my users (both Windows and Linux users) still keep comming to me with an issue that their data file is full of zero bytes after their OS crashed. I even tried adding sleep()
after the newFile.close()
but it had no effect.
I asked on Qt forum and was told that Qt does not do any caching or buffering internally and everything is passed to the OS right away, so if there is caching involved, it must be on the of OS or hardware.
My question is: What is the best practice when applications want to save data files containing for exampple user settings? What can i do to reduce the frequency of user data loss to minimum?