I have a logging application wherin I create a ofstream object say,
ofstream logger;
by doing this
logger.open(/* some path*/,ios_base:app);
and then i do some logging by writing to this ofstream object like this
logger << buffer << endl;
here buffer is some string object
now my problem is that how to accurately check for write failure ( for
example in case when disc is full)
I have tried something like this
logger.fail();
& also
logger.bad();
but both these do not return true in case of write failure.i am not
sure if i can use these with o/p stream.
I am sure theres got to be a way but I am a novice in c++ so I need
some help in getting around this problem
thanks
shyam
which particular exception should i catch ?
can you give me an example using some pseudo code
regards
shyam
use !logger to check for any error, and then fail(), bad() and eof() to
narrow things down. These should work. If not, what platform and
compiler are you using? What exactly is the disk error you are causing
(e.g. is it disk full, or something else, such as removing a memory
stick halfway through the write operation)? You can also try "errno",
which may give you extra information in a non-portable manner.
Tom
if ( ! logger.good() ) {
// some error condition has made the logger unsuable
}
or just:
if ( ! logger ) {
// same thing
}
Or you can test at the input site:
if ( ! (logger << buffer << endl) ) {
// logging failed
}
Or you can turn on exceptions:
logger..exceptions(
io_base::eofbit | io_base::failbit | io_base::badbit );
Then if a problem occurs an io_base::failure object will be thrown.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Have you tested this?
What you "think" isn't helpful, particularly when it's wrong.
--
Richard Herring