Boost logo

Boost Users :

Subject: [Boost-users] [Iostreams] stream<file_sink> does not throw an exception and not set fail/bad bits when file is wrong
From: Mariusz Wojtysiak (zxspeccy.cpp_at_[hidden])
Date: 2010-02-08 04:52:50


Hello!

I'm trying to write some content to a new file using
boost::stream<file_sink> class.
Unfortunately errors/failures during opening the file are ignored by
boost library.

Here is a snippet:

using namespace boost::iostreams;
stream<file_sink> stream( "/wrong.txt" );
std::cout << stream.fail() << ' ' << stream.bad() << std::endl;
stream.exceptions( std::ios::badbit | std::ios::failbit );
stream << "test" << std::endl;
stream.close();

Program is started as not root user and have not access for file /
wrong.txt.
It should throw an exception, but instead of this:
- console prints: 0 0
- none exception is thrown
- file /wrong.txt is not created

Similar piece of code using std::ofstream works as I expect:
using namespace std;
ofstream stream( "/wrong.txt" );
std::cout << stream.fail() << ' ' << stream.bad() << std::endl;
stream.exceptions( std::ios::badbit | std::ios::failbit );
stream << "test" << std::endl;
stream.close();

- console prints: 1 0
- exception is thrown

I cannot believe that boost::iostreams has such obvious bug and just
ignore failures!
I have to do something wrong. But what?

Regards,
Mariusz Wojtysiak


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net