c++ streaming into char array? -


im working on c++ application using c library needs callback implemented side example callback defined this:

int onwrite(char *what, char *buffer, size_t size, off_t offset); 

i have std::set needs formated , written buffer , take care of size , offset size buffer's size , offset how many bytes have been processed

is there c++ way can stream-like buffer? think this:

somestlstandardstream stream(buffer,size); stream.ignore(offset);  // drop first n offset bytes for(it=dataset.begin();it!=dataset.end() && stream.buffer_avail()>0;it++)     stream << *it << "|"; // format csv example 

the core question have is: there standard c++ way grab slice of formated/streamed data , write given buffer?

if not ill have implement own

i had @ stringstream doesnt seem fit needs cause using own write buffer instead of mine , dont know how many bytes have been written , such....

thx

you can allocate memory , wrap in string stream using std::stringstream::pubsetbuf:

 int main()  {     char buf[1024];      std::stringstream stream;      stream.rdbuf()->pubsetbuf(buf, sizeof(buf));      stream<< "hello " << "world " << std::endl;      std::string str;     std::getline(stream, str);     std::cout << str << std::endl;      //... } 

read std::stringstream, std::basic_streambuf , std::basic_stringbuf.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -