class KSaveFile


Module kdecore
Namespace
Class KSaveFile
Inherits QFile
\class KSaveFile ksavefile.h

Class to allow for atomic file I/O, as well as utility functions.

The KSaveFile class has been made to write out changes to an existing file atomically. This means that either ALL changes will be written to the file, or NO changes have been written, and the original file (if any) has been unchanged. This is useful if you have lots of time-consuming processing to perform during which an interruption could occur, or if any error in the file structure will cause the entire file to be corrupt.

When you create a KSaveFile for a given file, a temporary file is instead created and all your I/O occurs in the save file. Once you call finalize() the temporary file is renamed to the target file, so that all your changes happen at once. If abort() is called then the temporary file is removed and the target file is untouched. KSaveFile derives from QFile so you can use it just as you would a normal QFile.

This class also includes several static utility functions available that can help ensure data integrity. See the individual functions for details.

Here is a quick example of how to use KSaveFile:

First we create the KSaveFile and open it.

KSaveFile saveFile;
saveFile.setFileName("/lib/foo/bar.dat");
if ( !saveFile.open() ) {
//Handle error
}

At this point the file "/lib/foo/bar.dat" has not been altered in any way. Now, let's write out some data to the file.

QTextStream stream ( &saveFile );
stream << "Add some data.";
// Perform long processing
stream << "Add some more data.";
stream.flush();

Even after writing this data, the target file "/lib/foo/bar.dat" still has not been altered in any way. Now that we are done writing our data, we can write out all the changes that we have made by calling finalize().

if ( !saveFile.finalize() ) {
//Handle error
}

If a user interruption or error occurred while we were writing out our changes, we would instead call abort() to cancel all the I/O without affecting the target file.

See also QFile

Author Jaison Lee Author Waldo Bastian



methods