"Bad Bit"

This is a discussion on "Bad Bit" within the C++ Programming forums, part of the General Programming Boards category; I'm currently experimenting around with how to break things within the bits in a stream. As far as I know ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    "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?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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";
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen returning "Bad File Pointer"
    By jprukop in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21