How to write data from stringstream to a file (CPP)
<p>I have one function which is an call back from a library it looks like follows:  </p>

<pre><code>void onCallBack(int date, const std::stringstream &amp;data); 
</code></pre>

<p>I want to write data received from a <code>data</code> variable to an physical file so i am doing this:  </p>

<pre><code>void onCallBack(int date, const std::stringstream &amp;data)
{
    ofstream filePtr;
    filePtr.open("data.file", ios::app);

    string dataToWrite = data.str();
    filePtr &lt;&lt; dataToWrite.c_str();

    filePtr.close();
}
</code></pre>

<p>call back <code>onCallBack</code> function gets called when ever there is an update in data and i want to write this updated data to the file. </p>

<p>problem is that data is <code>std::stringstream</code> type and it behaves like an file/buffer and and from this buffer i just want to read update data part for example:</p>

<p>In first call back <code>data</code> contains string<br>
<code>this is first line</code>    </p>

<p>and in second call back it contains:<br>
<code>this is first line<br>
this is second line</code></p>

<p>In the first call of the call back function I write <code>this is first line</code> string to the file and in second call back I just want to write <code>this is second line</code> to file not the fist line again.   </p>

<p>How can i extract only updated part of the <code>std::stringstream</code>.  </p>

<p><code>const std::stringstream &amp;data</code> variable is constant and can't be modified or we can't use <code>tellg</code> or <code>sync</code>.</p>

<p>UPDATE/EDIT:<br>
1. Sorry for c tag.<br>
2. for using <code>read</code> we need to provide block size to read and i don't know the block size.<br>
3. Can you provide an example of doing this using ios_base::xalloc, ios:base::iword, and ios_base::pword.<br>
4. read is not a const but tellg is.<br>
5. yes no one call <code>data.str("")</code>, it is an pure virtual function from lib, in my code i am not doing this. </p>
