Results 1 to 4 of 4

Thread: Backing up a file.

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Backing up a file.

    Hi,

    I have a application that uses Sqlite to store data and also the option to backup this data.

    I wrote the following code but is not working.

    Qt Code:
    1. QString filter = tr("Database Backup (*.dbb)");
    2. QString fileName = QFileDialog::getSaveFileName(this,tr("Backup Database"),QDir::homePath(),filter);
    3.  
    4. if(!fileName.isEmpty()){
    5. QString filePath = QDir::homePath();
    6. filePath.append("/database");
    7. QFile sourceFile(filePath);
    8. if(!sourceFile.open(QIODevice::ReadOnly)){
    9. //Error Message
    10. return;
    11. }
    12. QFile destFile(fileName);
    13. if(!destFile.open(QIODevice::WriteOnly)){
    14. //Error Message
    15. return;
    16. }
    17. QTextStream sourceStream(&sourceFile);
    18. QTextStream destStream(&destFile);
    19. QByteArray data;
    20. sourceStream>>data;
    21. data = qCompress(data);
    22. destStream<<data;
    23. sourceFile.close();
    24. destFile.close();
    25. }
    To copy to clipboard, switch view to plain text mode 

    The destination file is getting created but it's size is only 10 Bytes.

    Can someone please tell me what is wrong with the code ?

    Thanks a lot.
    Last edited by munna; 30th September 2006 at 10:51. Reason: Spelling mistake

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Backing up a file.

    Is this a text file? Because if not, you shouldn't use QTextStream. And even if it is a text file, you shouldn't use QTextStream with the stream operator (you'll lose whitespaces, etc.).

    I guess this would be a bit better:

    Qt Code:
    1. while(!sourceFile.atEnd()){
    2. data = sourceFile.read(64*1024); // read 64kB
    3. data = qCompress(data);
    4. destFile.write(data);
    5. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    munna (30th September 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Backing up a file.

    With your code, how will the reverting back of the data work ?

    Can I use the following code to get back the original file

    Qt Code:
    1. QByteArray umcompressedData;
    2. while(!compressedFile.atEnd()){
    3. umcompressedData = compressedFile.read(64*1024); // read 64kB. Can I do this ??
    4. umcompressedData = qUncompress(umcompressedData);
    5. destFile.write(umcompressedData);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I think reverting back the data will be a problem.

    What can I do ?

    Thanks.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Backing up a file.

    Try to uncompress with 64k blocks. zlib works on streams, afaik, so there is a chance this will work. But if it doesn't just use sourceFile.readAll() instead of reading 64k at a time. It'll use more memory but will certainly work correctly when uncompressing.

Similar Threads

  1. Draging a non-existing file to the Windows Desktop
    By klaus1111 in forum Qt Programming
    Replies: 13
    Last Post: 20th September 2007, 11:47
  2. Qt 3.3.4 -> Qt 4.1.2, moc file issue
    By philski in forum Qt Tools
    Replies: 1
    Last Post: 11th September 2006, 21:08
  3. .ui file name and classname
    By Rekha in forum Newbie
    Replies: 3
    Last Post: 12th August 2006, 01:53
  4. SQLite-DB in a qrc file
    By Lykurg in forum Qt Programming
    Replies: 5
    Last Post: 31st July 2006, 19:24
  5. QHttp GET File & Password
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2006, 13:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.