Questions tagged [streambuf]

std::streambuf is the stream buffer type used by C++ iostreams.

C++ iostreams use a stream buffer to do any buffering of characters, manage the stream position and transport characters to/from an external device such as a file.

Use this tag for questions about std::streambuf, the wide-character equivalent std::wstreambuf, or the class template std::basic_streambuf. The tag might also be relevant.

152 questions
57
votes
2 answers

C++ streams confusion: istreambuf_iterator vs istream_iterator?

What is the difference between istreambuf_iterator and istream_iterator? And in general what is the difference between streams and streambufs? I really can't find any clear explanation for this so decided to ask here.
dextrey
  • 815
  • 7
  • 9
40
votes
11 answers

Copy a streambuf's contents to a string

Apparently boost::asio::async_read doesn't like strings, as the only overload of boost::asio::buffer allows me to create const_buffers, so I'm stuck with reading everything into a streambuf. Now I want to copy the contents of the streambuf into a…
tstenner
  • 10,080
  • 10
  • 57
  • 92
25
votes
3 answers

How do I create my own ostream/streambuf?

For educational purposes I want to create a ostream and stream buffer to do: fix endians when doing << myVar; store in a deque container instead of using std:cout or writing to a file log extra data, such as how many times I did <<, how many…
user34537
16
votes
1 answer

How to create stream which handles both input and output in C++?

I'm trying to make a class that will be both input and output stream (like std::cout and std::cin ). I tried to overload operator << and >>, but then, I understood that writing such code is not wise to do (as this would be an approach to rewrite C++…
Akib Azmain Turja
  • 1,142
  • 7
  • 27
16
votes
4 answers

initializing a C++ std::istringstream from an in memory buffer?

I have a memory block (opaque), that I want to store in a Blob in mySQL through their C++ adapter. The adapter expects a istream: virtual void setBlob(unsigned int parameterIndex, std::istream * blob) = 0; So my question is: how can I create a…
Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71
15
votes
1 answer

Inheriting from std::basic_streambuf to write to a socket

I would like to write a logging library of my own that provides abstraction for wherever the log entries are sent to. The IO library of C++ already provides that kind of abstraction with std::stringstream and std::fstream. I would also like to be…
Virus721
  • 8,061
  • 12
  • 67
  • 123
15
votes
1 answer

Use streambuf as buffer for boost asio read and write

I'm using this code for reading socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&session::handle_read, this, boost::asio::placeholders::error, …
user1307957
  • 541
  • 3
  • 8
  • 19
13
votes
1 answer

Working with boost::asio::streambuf

Looking for a boost::asio (and with himself boost) decided to write asynchronous server. To store incoming data I use boost::asio::streambuf. Here I have a problem. When I receive a second message from the client and subsequent I see that in the…
vint
  • 161
  • 1
  • 2
  • 5
11
votes
1 answer

boost::asio read n bytes from socket to streambuf

I have a serialized structure, which is being sent via socket. I need to read it in chunks, since one of its fields contains the size of the data remaining: I need to read first few bytes, find out the length and read the rest. This is what I have…
10
votes
1 answer

Difference between "internal" vs "associated" stream buffer

From http://www.cplusplus.com/reference/ios/ios/rdbuf/: Some derived stream classes (such as stringstream and fstream) maintain their own internal stream buffer, to which they are associated on construction. Calling this function to change the…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
10
votes
1 answer

Block-level copying of data between streambuffers

I would like to copy data efficiently between std::streambuf instances. That is, I would like to shovel blocks of data between them, as opposed to perform character-by-character copying. For example, this is not what I am looking for: stringbuf…
mavam
  • 12,242
  • 10
  • 53
  • 87
10
votes
3 answers

Should I create a temporary ostream using another's streambuf?

Suppose I have a function that takes an ostream & parameter o and writes to that ostream. An operator << implementation would be a good example. ostream& operator << (ostream& o, const MyThing& t) { // ... interesting code here ... return…
peterpi
  • 575
  • 2
  • 11
9
votes
2 answers

Reading from serial port with Boost Asio

I'm want to check for incoming data packages on the serial port, using boost.asio. Each data packet will start with a header that is one byte long, and will specify what type of the message has been sent. Each different type of message has its own…
HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57
9
votes
1 answer

boost streambuf consume and commit, what is it

I can't seem to find a good explanation of what consume() and commit() really means, actually I don't understand streambuf at all. My understanding is that a streambuf is just a character array. But why is that in the documentation,…
seilgu
  • 364
  • 3
  • 14
8
votes
2 answers

basic_streambuf::seekoff what should be returned when ios_base::in | ios_base::out is specified?

27.6.3.4.2 Buffer management and positioning pos_type seekoff(off_type off, ios_base::seekdir way, ios_base::openmode which = ios_base::in | ios_base::out); Effects: Alters the stream positions within one or more of the controlled sequences…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1
2 3
10 11