Results 1 to 1 of 1
When I do Qt programs I have a habit of using Qt classes rather than STL classes because, frankly, Qt is much better documented. (I have yet to find a ...
- 03-27-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 20
Making backup files for Qt programs with QFile
When I do Qt programs I have a habit of using Qt classes rather than STL classes because, frankly, Qt is much better documented. (I have yet to find a good, comprehensive book/resource that can be self-contained on my computer for STL. Anyone want to recommend one?) I am currently making a simple Qt program to help me organize a library (i.e. store records of books and be searchable by a number of different keys) and save the list of books into a csv file. (I know there are probably other programs that do something similar, but I don't want something with a huge commercial GUI that takes ages to load.)
Naturally, if the program (or OS) has some kind of fatal crash while it's saving, I'd like to have a backup around, so I don't have to do all that data entry again. Basically, before the save happens, the original file is copied into <filename>.bak, and then the original file is overwritten with the new list. But I can't get QFile to write the backup, and often it won't make the save file correctly either. What am I missing?
I've also tried using QFile::rename (backupName) and QFile::copy (backupName), but those didn't seem to work. The output to the terminal (which should match the output to the backup file is all correct, which means that it's reading the original file alright, but the file write never happens. What's going on?Code:bool writeFile (QString &filename) { QTextStream terminal (stdout); QFile file (filename); QString backupName = filename + ".bak"; if (!file.open (QIODevice::ReadOnly | QIODevice::Text)) return false; if (!backupFile.open (QIODevice::WriteOnly | QIODevice::Text)) return false; QTextStream readFrom (&file); QTextStream backup (&backupFile); while (!readFrom.atEnd ()) { QString line = ""; do { line = readFrom.readLine (); terminal << line << endl; backup << line << endl; } while (line == ""); } backupFile.close (); file.close (); if (!file.open (QIODevice::WriteOnly | QIODevice::Text)) return false; QTextStream out (&file); while (!bookList.isEmpty ()) { // write first element of list out in csv format, and // then remove that element from the list } file.close (); return true; }


Reply With Quote