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
8
votes
5 answers

redirect std::cout to a custom writer

I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include #include #include #include int main() { std::ostringstream oss; // Make clog use the buffer from oss …
Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
8
votes
3 answers

What can go wrong if cout.rdbuf() is used to switch buffer and never set it back?

The author presented this code under the title A bus error on my platform #include #include int main() { std::ofstream log("oops.log"); std::cout.rdbuf(log.rdbuf()); std::cout << "Oops!\n"; return 0; } The…
John Kalane
  • 1,163
  • 8
  • 17
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
  • 42,585
  • 14
  • 91
  • 146
7
votes
1 answer

istream::tellg() returns -1 when used with my custom streambuf class?

I'm trying to create an istream that reads directly from a raw memory buffer. I found a nice way to do this in another post on here: class membuf : public basic_streambuf { public: membuf(char* p, size_t n) { setg(p, p, p…
EdSanville
  • 113
  • 7
7
votes
1 answer

inheriting ostream and streambuf problem with xsputn and overflow

I have been doing research on creating my own ostream and along with that a streambuf to handle the buffer for my ostream. I actually have most of it working, I can insert (<<) into my stream and get strings no problem. I do this by implimenting…
Digital Powers
  • 460
  • 6
  • 23
7
votes
2 answers

How to implement custom std::streambuf's seekoff()?

I have the following implementation based on e.g. this question and answer struct membuf : std::streambuf { membuf(char* begin, char* end) { this->setg(begin, begin, end); } protected: virtual pos_type seekoff(off_type off, …
Patryk
  • 22,602
  • 44
  • 128
  • 244
7
votes
1 answer

How do I implement seekg() for a custom istream/streambuf?

I used to be a C++ expert a decade ago, but for the past 10 years I've been programming Java. I just started a C++ project that uses a small third-party XML parser. The XML parser accepts an STL istream. My XML data is coming from a Windows COM…
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
7
votes
2 answers

Use multiple ofstreams to write to a single output file in c++

I have class Writer that has two ofstream members. Both streams are associated with the same output file. I'd like to use both streams in Writer::write method, but to make sure that each stream writes to the end of the real output file. Code class…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
7
votes
4 answers

Threadsafe logging

I want to implement a simple class for logging from multiple threads. The idea there is, that each object that wants to log stuff, receives an ostream-object that it can write messages to using the usual operators. The desired behaviour is, that the…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
6
votes
1 answer

Code using boost::asio::streambuf causes segfault

I've experienced problems using asio::streambuf and am hoping someone can tell me if I'm using the class incorrectly. When I run this example code it segfaults. Why? To make things more confusing, this code works on Windows (Visual Studio 2008), but…
Dylan Klomparens
  • 2,853
  • 7
  • 35
  • 52
6
votes
2 answers

Is it possible to change ostringstream rdbuf?

I try the following code: ostringstream oss; streambuf *psbuf, *backup; backup = oss.rdbuf(); psbuf = cout.rdbuf(); oss.rdbuf(psbuf); oss << things << endl; oss.rdbuf(backup); But unfortunately I get this error: error: too…
user3391320
  • 1,023
  • 7
  • 9
5
votes
2 answers

Using std::endl on an ostream makes my file binary

I am working on a project that uses libzip. I'm working in c++14 and I wrote a tiny wrapper around libzip to make my life easier. I have an std::ostream object built around custom class that inherits std::streambuf. This streambuf uses the libzip…
Ebatsin
  • 552
  • 5
  • 17
5
votes
1 answer

rdbuf(...) returns a pointer -- who owns the buffer pointed to?

There is a STL library function streambuf* std::basic_ios::rdbuf (streambuf* sb); This function can be used to change the stream buffer associated with a stream. If you use this, it returns a pointer to the streambuf that was previously in…
Mohan
  • 7,302
  • 5
  • 32
  • 55
5
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…
5
votes
3 answers

streams, stream_bufs, codecvt facets and \n to \r\n translation

What part of the C++ IO streams does the \r to \r\n conversion? Is it the stream_buf itself or is it part of the internal to external encoding conversion by codecvt facet? UPDATE 1 You all say that it is done in streambuf/filebuf. Ok. But how does…
wilx
  • 17,697
  • 6
  • 59
  • 114
1
2
3
10 11