Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Safe solution to serialize data
Qt 6.11 is out! See what's new in the release blog

Safe solution to serialize data

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 928 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #1

    I want to serialize a bunch of data by QDataStream and read them after, my question is, how could I make sure the file only updated if all of the data has been written?

    QFile file("proxy_settings.dat");
    if(!file.open(QIODevice::WriteOnly)){
        QMessageBox::warning(nullptr, tr("Error"), tr("Cannot write proxy data into file, your settings "
                                                      "of the proxies cannot be saved"));
        return;
    }
    QDataStream stream(&file);
    for(int i = 0; i != ui->tableWidgetPoxyTable->rowCount(); ++i){
        //do something and dump data into file
        stream<<type<<host<<port<<user_name<<password;
    }
    

    This naive solution work if everything going right, but it will generate corrupt file if the program exit before the data are written. Do I have an easy way to make sure all of the data can be written? Thanks

    jsulmJ 1 Reply Last reply
    0
    • thamT tham

      I want to serialize a bunch of data by QDataStream and read them after, my question is, how could I make sure the file only updated if all of the data has been written?

      QFile file("proxy_settings.dat");
      if(!file.open(QIODevice::WriteOnly)){
          QMessageBox::warning(nullptr, tr("Error"), tr("Cannot write proxy data into file, your settings "
                                                        "of the proxies cannot be saved"));
          return;
      }
      QDataStream stream(&file);
      for(int i = 0; i != ui->tableWidgetPoxyTable->rowCount(); ++i){
          //do something and dump data into file
          stream<<type<<host<<port<<user_name<<password;
      }
      

      This naive solution work if everything going right, but it will generate corrupt file if the program exit before the data are written. Do I have an easy way to make sure all of the data can be written? Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @tham What you can do: first write to a temporary file. If you finish successfully then move (don't copy) this temporary file to its correct location (overwrite the old one). If your app crashes, then the old file will be still untouched. The temporary file should be on the same partition (in the same file system), then moving it is very fast as the file system does not need to copy its content.
      It can look like this during writing:

      some_dir/
          original_file.dat
          temp_file.dat
      

      After writing delete original_file.dat and move (or rename if both files are in the same directory) temp_file.dat to original_file.dat.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        This is exactly what QSaveFile is for http://doc.qt.io/qt-5/qsavefile.html

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved