closing stream before opening sets fail bit

llewelly@dbritsch.dsl.xmission.com llewelly@dbritsch.dsl.xmission.com
Sat Jun 17 18:02:00 GMT 2000


I cannot tell if libstdc++-v3 is std conforming in this respect, but I
think ofstream::clear() might help you.


On Sat, 17 Jun 2000, Rob Willis wrote:

> 
> 
> Found this quirky behavior. Is this the correct behavior? Basically, calling
> close() before opening the ofstream causes the fail bit to be
> set. I can live with that... But then, the open command succeeds and the
> fail bit is still set!!  And of course the write fails.
> 
> Try this sample:
>  (i'm using the egcs-20000612 image of both gcc and libstdc++-v3)
> 
>  #include <fstream>
>  #include  <iostream>
> 
>  int main()
>  {
>    std::ofstream ofs1;
>    ofs1.close();

I tried adding 'ofs1.clear()' after this line and got:

> 
>    // false as expected:

false (as before)

>    std::cout << ofs1.is_open() << std::endl;
>    // this is now true:

false (because failbit was clear()ed)

>    std::cout << !(ofs1) << std::endl;
> 
>    ofs1.open( "junk.tmp" );
> 
>    // is_open is now true !?!


true (as before)

>    std::cout << ofs1.is_open() << std::endl;
>    // fail bit still true


false

>    std::cout << !(ofs1) << std::endl;
> 
>    // this fails...


this succeeds.

>    ofs1 << "junk" << std::endl;
>  }
> 
> --
> Rob Willis
> rob@e-critical.com
> 760-822-1439
> 
> 



More information about the Libstdc++ mailing list