Results 1 to 3 of 3

Thread: Populate QDataStream and only write to QIODevice under a certain condition

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Populate QDataStream and only write to QIODevice under a certain condition

    Hi

    What would the best way be to do this:

    I have a QDataStream which saves project data to a file. Currently I do it like this:

    Qt Code:
    1. QFile file(file_name);
    2. file.open(QIODevice::WriteOnly);
    3. QDataStream stream(&file); // we will serialize the data into the file
    4. // Now I stream lots of stuff to the stream.
    To copy to clipboard, switch view to plain text mode 

    However sometimes something goes wrong in the project saving and in that case I still need the original file. One way I can think of doing it is to write out a temp file and when successfull copy it over the current file. But I'm sure there must be a better way.

    Can I create the stream and then flush it to the file all at once when everything went alright? Or what is the recommended way to do this.

    Thanks,
    Jaco

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Populate QDataStream and only write to QIODevice under a certain condition

    Your definition of "something goes wrong" is flawed.

    In your definition, you only take into account problems while constructing the datastream.
    However, writing the datastream to a file can go wrong too.

    In other words, even if your datastream is completely correct, writing it to a file can result in a corrupt file when, for example, there's not enough disk space or when there are conflicts in who has access to the file etc...

    Therefor, your suggestion to work with temporary files is the best way to check if the saved file is correct.

    You could use a version control system too of course, like svn etc...

  3. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Populate QDataStream and only write to QIODevice under a certain condition

    Thanks! Good point

Similar Threads

  1. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  2. Replies: 1
    Last Post: 16th June 2009, 09:09
  3. How to populate delegate with model data.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 09:31
  4. Sleep condition in QT
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2008, 12:07
  5. Using QSettings to populate a QComboBox
    By nbkhwjm in forum Newbie
    Replies: 16
    Last Post: 4th September 2007, 22:34

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.