I have one function which is an call back from a library it looks like follows:
void onCallBack(int date, const std::stringstream &data);
I want to write data received from a data variable to an physical file so i am doing this:
void onCallBack(int date, const std::stringstream &data)
{
ofstream filePtr;
filePtr.open("data.file", ios::app);
string dataToWrite = data.str();
filePtr << dataToWrite.c_str();
filePtr.close();
}
call back onCallBack function gets called when ever there is an update in data and i want to write this updated data to the file.
problem is that data is std::stringstream type and it behaves like an file/buffer and and from this buffer i just want to read update data part for example:
In first call back data contains string
this is first line
and in second call back it contains:
this is first line
this is second line
In the first call of the call back function I write this is first line string to the file and in second call back I just want to write this is second line to file not the fist line again.
How can i extract only updated part of the std::stringstream.
const std::stringstream &data variable is constant and can't be modified or we can't use tellg or sync.
UPDATE/EDIT:
1. Sorry for c tag.
2. for using read we need to provide block size to read and i don't know the block size.
3. Can you provide an example of doing this using ios_base::xalloc, ios:base::iword, and ios_base::pword.
4. read is not a const but tellg is.
5. yes no one call data.str(""), it is an pure virtual function from lib, in my code i am not doing this.
readinstead ofstr, which, presumably removes the read characters from the buffer.data.str("")etc.? Did you consider inheriting from the string stream buffer, hooking intoxsputn,overflow, and/or whatever else may be required? (I haven't checked the details on this, hence no answer.)