-
"Bad Bit"
I'm currently experimenting around with how to break things within the bits in a stream. As far as I know there are 3 "naughty bits". There is a "fail bit", an "end of file bit" and a "bad bit". As far as I can decipher the "bad bit" is due to a hardware failure. I have tried EVERYTHING that I can think of to try and trip the "bad bit" and I can't seem to accomplish it. Does anybody have any insight?
-
The badbit is set after a system failure affecting the stream. It's not easy to deliberatly provoke it. It's usually not possible to recover from it, so this is an error that should originate a program failure and termination.
-
Write your custom stream insertion/extraction operator and throw an exception from that:
Code:
#include <iostream>
#include <stdexcept>
class dummy {};
ostream & operator << (ostream &, const dummy &)
{
throw std::runtime_error("gotcha")<
}
int main()
{
try {
std::cout << dummy();
} catch(std::runtime_error &) {
}
bool b = std::cout.fail();
std::cout.clear();
std::cout << "Failbit: " << b << "\n";
}