<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jun 27, 2013 at 1:09 PM, Nishant Parashar <span dir="ltr"><<a href="mailto:nishsites@gmail.com" target="_blank">nishsites@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Since the data is very less. Read the file once during start-up of app and prepare the in-memory QMap. This QMap is used for any set/get calls by the app, don't read/write to file. Finally when we exit the app, write the QMap to file wiping out old contents.<br>
</blockquote><div><br></div><div>The only problem here is that if the app unexpectedly quits, then I've lost this data. As I mentioned earlier, since this data is fetched over the n/w I'd rather commit it immediately.<br>
<br></div><div>Thanks,<br></div><div>-mandeep<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<br><div class="gmail_quote"><div><div class="h5">On 27 June 2013 12:26, Mandeep Sandhu <span dir="ltr"><<a href="mailto:mandeepsandhu.chd@gmail.com" target="_blank">mandeepsandhu.chd@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">

<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div><div>Hi,<br><br></div><div>I wanted to know if I'm doing file i/o the right way in one of my apps.<br></div><div><br>


</div>
I'm writing  a small helper class for one of my applications which is supposed to store data, in the form of key-value pairs, in a file. This is very similar to QSettings but w/o any fancy groups/sections that it provides. So lets assume that I have to use a file based approach only! :)<br>



<br></div>It provides 2 functions - get(key) and a set(key, value) and they behave exactly like QMaps equivalent functions, i.e if the key exists, it'll overwrite the value, else it'll add a new entry. This class takes either a filename as input or a QFile itself.<br>



<br></div><div>Assumptions:There won't be much data written to the file (max 1-2 KB). The set function will not be called very often (probably once or twice in the lifetime of the app). The get fxn will be called a little more often.<br>



</div><br></div>Here's what I do in the 2 functions (skipping error condition checks keep it simple):<br><br></div>get(key):<br></div>* Open the file in Read-Only mode.<br></div>* Parse the data and prepare a QMap of the key-value pairs.<br>



</div>* Close the file<br></div>* Return the value for the key<br><br></div>set(key, value):<br>* Open the file in Read-Only mode.<br>* Parse the data and prepare a QMap of the key-value pairs.<br>* Close the file<br></div>



* Open the file in Write-Only & Truncate mode<br></div><div>* Insert the new key-value in the map<br></div>* Write the map<br></div>* Close the file<br><br></div>In the set function I'm opening the file twice - once to read it's content and prepare the map and next I open it in truncate mode as I want to wipe off the existing data and write the new data afresh.<br>



<br></div>Is there a simpler way to achieve what I'm doing with a file? I'm assuming the 2 open-close calls in set() are ok as it won't be called very often.<br><br></div>Thoughts?<br><br></div>Thanks,<br></div>



-mandeep<br><br></div>
<br></div></div><div class="im">_______________________________________________<br>
Interest mailing list<br>
<a href="mailto:Interest@qt-project.org" target="_blank">Interest@qt-project.org</a><br>
<a href="http://lists.qt-project.org/mailman/listinfo/interest" target="_blank">http://lists.qt-project.org/mailman/listinfo/interest</a><br>
<br></div></blockquote></div><br>
</blockquote></div><br></div></div>