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.

141 questions
50
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
  • 745
  • 6
  • 9
36
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
  • 9,119
  • 10
  • 48
  • 83
24
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
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,602
  • 7
  • 40
  • 67
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
  • 511
  • 2
  • 8
  • 19
13
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
  • 851
  • 3
  • 25
12
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
  • 7,156
  • 8
  • 49
  • 110
11
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
  • 141
  • 1
  • 2
  • 5
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
  • 11,144
  • 9
  • 46
  • 83
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
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
  • 11,461
  • 15
  • 67
  • 116
9
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…
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
  • 14,584
  • 15
  • 60
  • 93
8
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
  • 2,752
  • 6
  • 32
  • 43
8
votes
5 answers

Binary version of iostream

I've been writing a binary version of iostreams. It essentially allows you to write binary files, but gives you much control over the format of the file. Example usage: my_file << binary::u32le << my_int << binary::u16le << my_string; Would write…
Thanatos
  • 37,926
  • 14
  • 76
  • 136
1
2 3
9 10