Topic: ios::badbit set when rdstate & exceptions() causes throw ios::failure?


Author: sebormartin@netscape.net (Martin Sebor)
Date: Fri, 21 May 2004 22:37:58 +0000 (UTC)
Raw View
..
> Can someone verify that this is correct behavior?

No, it's not correct.

> If so, can someone
> explain to me why badbit is being set in this case?

Because of a libstdc++ bug. AFAICS, it's fixed in the latest version
of the library that comes with gcc 3.4.

Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: aaronblue6@cox-internet.com ("Aaron W. LaFramboise")
Date: Sat, 15 May 2004 18:43:55 +0000 (UTC)
Raw View
Hello,

On my implementation (GCC 3.3.2 on Win32), badbit is set as well as
failbit and eofbit when I reach the end of an ifstream if and only if
(exceptions() & eofbit) == true.  See the following test case:

----
#include <fstream>
#include <iostream>


int main() {

   using std::cout;

   std::ifstream in;

   in.open("/dev/null");
//  in.open("nul");

   using std::ios;

   // comment the following line
   in.exceptions(ios::failbit | ios::badbit | ios::eofbit);

   try {
     in.get();
   }
   catch(ios::failure &e) {
     cout << e.what() << '\n';
   }

   if(in.good())
     cout << "good ";
   if(in.bad())
     cout << "bad ";
   if(in.fail())
     cout << "fail ";
   if(in.eof())
     cout << "eof";
   cout << '\n';

   return 0;
}
----

On my implementation, badbit is set when this program is compiled as-is,
but badbit is not set when "in.exceptions(..." is removed.

Can someone verify that this is correct behavior?  If so, can someone
explain to me why badbit is being set in this case?


Aaron W. LaFramboise

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]