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
4
votes
1 answer

boost asio streambuf don't release memory after calling consume?

boost::asio::streambuf b; ... void handler(const boost::system::error_code& e, std::size_t size) { if (!e) { std::stringstream sstr(std::string((std::istreambuf_iterator(&b)), std::istreambuf_iterator())); …
user869210
  • 231
  • 1
  • 8
4
votes
3 answers

How to read from Boost ASIO streambuf?

Is there a way to read from a streambuf without removing the bytes? I'm reading a 'message size' field from the buffer to check if the whole message was received. If not, I'm posting another async read to get it, but the handler then has no way to…
James
  • 1,430
  • 4
  • 20
  • 27
4
votes
1 answer

serializing data to a std::streambuf

I have a Visual Studio 2008 C++ project where I'm trying to serialize data from several classes to a custom std::streambuf implementation. The data classes with their serialization: struct Header { /*...*/ }; inline std::ostream& operator<<(…
PaulH
  • 7,759
  • 8
  • 66
  • 143
4
votes
2 answers

Is it possible to "prepare" input from cin?

In his answer, specifically in the linked Ideone example, @Nawaz shows how you can change the buffer object of cout to write to something else. This made me think of utilizing that to prepare input from cin, by filling its streambuf: #include…
Xeo
  • 129,499
  • 52
  • 291
  • 397
4
votes
0 answers

Is read after EOF UB?

I try to combine Poco and boost::iostreams::filtering_stream and I found two problems: Poco::Net::HTTPChunkedStreamBuf::underflow returns EOF only once after session was closed. boost::iostreams::detail:indirect_streambuf::underflow: return value…
dahek
  • 41
  • 3
4
votes
1 answer

Why is the "gptr" type of basic_streambuf char_type* rather than const char_type*?

The basic_streambuf member to set the three "gptrs" of the streambuf, setg, is declared as: protected: void setg(char_type *gback, char_type *gptr, char_type *egptr); I am wondering: why was the type of each gptr made char_type* instead of const…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
4
votes
1 answer

Custom input stream. Stream buffer and underflow method

To understand how input streams work I designed 2 of the following classes: #include class my_streambuf: public std::streambuf { private: std::streambuf* buffer; char ch; protected: virtual std::streambuf::int_type underflow() …
HighPredator
  • 790
  • 4
  • 21
4
votes
3 answers

Adapter for std streams

I have an abstract class in my project, its derivatives is used for input/output to different locations. It has virtual methods read and write. virtual unsigned read(void *buf, unsigned len) = 0; virtual void write(const void *buf, unsigned len) =…
max_hassen
  • 387
  • 1
  • 5
  • 16
4
votes
4 answers

Deriving from streambuf without rewriting a corresponding stream

Some days ago, I decided that it would be fun to write a streambuf subclass that would use mmap and read-ahead. I looked at how my STL (SGI) implemented filebuf and realized that basic_filebuf contains a FILE*. So inheriting from basic_filebuf is…
NewbiZ
  • 2,395
  • 2
  • 26
  • 40
4
votes
1 answer

copying from a std::istreambuf_iterator<> to a std::vector<>

I have a Visual Studio 2008 C++ application where I would like to treat a stream as a set of iterators. For example, if I were to receive an array of WIN32_FIND_DATA structures over the stream, I would like to be able to do something like…
PaulH
  • 7,759
  • 8
  • 66
  • 143
4
votes
1 answer

Advantage of asio::streambuf over raw array

I don't quite understand the advantage of using streambuf over the regular array. Let me explain my problem. I have a network connection which is encrypted using Rijndael 128 ECB + some easy cipher to encrypt remaining data that is shorter than 16…
Schnappi
  • 125
  • 2
  • 10
4
votes
1 answer

Cannot set the streambuf of an ostringstream object

I want to include some std::ostringstream objects in my program for logging and error reporting purposes. Based on a setting given at compile time, the log and error streams will either collect their respective data (to be saved or whatever) or they…
Chase
  • 370
  • 5
  • 12
4
votes
1 answer

boost::asio::streambuf shrink-to-fit?

The boost::asio::streambuf size will keep on increasing until consume() is called. Even after consume() is called, the memory used by the underlying buffer will never be released. For example: the following code first created a streambuf without…
John Crane
  • 371
  • 5
  • 14
3
votes
2 answers

Which buffer should be set by basic_streambuf::setbuf?

I am working on a basic_streambuf to handle reading and writing from/to a Winsock socket. Just like basic_filebuf, I am internally using a std::codecvt object to convert bytes read from the underlying socket to the char type of the "socket…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
3
votes
3 answers

std::fstream with multiple buffers?

You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream file("file", std::ios_base::binary | std::ios_base::out); if (file.is_open()) { file.rdbuf()->pubsetbuf(buf, BUFFER_SIZE); file <<…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
1 2
3
10 11