<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Safe solution to serialize data]]></title><description><![CDATA[<p dir="auto">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?</p>
<pre><code>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(&amp;file);
for(int i = 0; i != ui-&gt;tableWidgetPoxyTable-&gt;rowCount(); ++i){
    //do something and dump data into file
    stream&lt;&lt;type&lt;&lt;host&lt;&lt;port&lt;&lt;user_name&lt;&lt;password;
}
</code></pre>
<p dir="auto">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</p>
]]></description><link>https://forum.qt.io/topic/80111/safe-solution-to-serialize-data</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 12:55:13 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/80111.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 09 Jun 2017 02:07:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Safe solution to serialize data on Fri, 09 Jun 2017 06:56:41 GMT]]></title><description><![CDATA[<p dir="auto">This is exactly what QSaveFile is for <a href="http://doc.qt.io/qt-5/qsavefile.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qsavefile.html</a></p>
]]></description><link>https://forum.qt.io/post/398394</link><guid isPermaLink="true">https://forum.qt.io/post/398394</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 09 Jun 2017 06:56:41 GMT</pubDate></item><item><title><![CDATA[Reply to Safe solution to serialize data on Fri, 09 Jun 2017 04:45:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tham">@<bdi>tham</bdi></a> 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.<br />
It can look like this during writing:</p>
<pre><code>some_dir/
    original_file.dat
    temp_file.dat
</code></pre>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/398369</link><guid isPermaLink="true">https://forum.qt.io/post/398369</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Fri, 09 Jun 2017 04:45:40 GMT</pubDate></item></channel></rss>