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
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,467
  • 7
  • 63
  • 133
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
  • 123,374
  • 44
  • 277
  • 381
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
  • 35,770
  • 14
  • 104
  • 182
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
  • 760
  • 3
  • 19
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,218
  • 1
  • 24
  • 31
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
  • 115
  • 1
  • 9
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
  • 360
  • 4
  • 11
4
votes
1 answer

streambuf get streampos

I use the C++ streambuf class for a compiler project and need a convenient way to get the current position in the stream. There are two member functions, streambuf::pubseekpos and a streambuf::pubseekoff, to modify the position and I am quite…
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
  • 341
  • 5
  • 13
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
  • 35,770
  • 14
  • 104
  • 182
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
  • 14,584
  • 15
  • 60
  • 93
3
votes
1 answer

Is std::stringstream::flush() supposed to do anything?

std::ostream's have a flush() method which: Writes uncommitted changes to the underlying output sequence. What does that mean for an std::stringstream? If I understand correctly, it means there's nothing to be done for such a stream. Is this…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
3
votes
1 answer

CRTP call child function in destructor of parent

I have two classes structured like so (simplified the code to show the problem more clearly): template class Stream : public std::basic_streambuf> { private: std::string pBuffer; //other…
Brandon
  • 20,445
  • 9
  • 73
  • 162
3
votes
3 answers

What are 'aliased' stream buffers?

What are 'aliased stream buffers`? I encountered the term in a comment on an answer of mine.
xtofl
  • 38,207
  • 10
  • 95
  • 177
1 2
3
9 10