1

Lastly I did a lot of work to implement my own stream class in c++. The work is almost done, but there is one last thing...

You see I discovered in C++ input and output streams uses an internal std::streambuf class that is doing the dirty job of actual writing/reading the contents. My streambuf class is operating on files and during this sometimes it may enter a state where it is impossible to do any writing/reading (for example the requested file is not existing).

My problem is, when such that happen, I want to set my streambuf into a bad state. But I discovered that std::streambuf hasn't got setstate method. Only istream and ostream classes have it. But, as I said before, my streambuf is contained inside those classes.

So, how can I inform classes that have got my streambuf that I entered a bad state?

Felix.leg
  • 622
  • 1
  • 4
  • 13
  • Here's one example how errors are detected, and managed with certain return values: https://en.cppreference.com/w/cpp/io/basic_streambuf/overflow – πάντα ῥεῖ Oct 18 '20 at 08:20
  • @πάνταῥεῖ So all I can do is returning ```eof```? But how an owner can differentiate between my class finishing its reading and entering a bad state? – Felix.leg Oct 18 '20 at 08:26
  • Another way is to throw exceptions from any of the `std::streambuf` operations you'll provide. I've collected a number of duplicate links, which explain in more detail how the protocol of interaction between `std::iostream` and `std::streambuf` works. – πάντα ῥεῖ Oct 18 '20 at 09:51

0 Answers0