By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

how to check write failure in ofstream??

P: n/a
HI

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

Apr 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
hi,shyam,i think if write failure,the system will throw a exception, so
you may use try..catch to catch this exception.

Apr 11 '06 #2

P: n/a

veldwolf wrote:
hi,shyam,i think if write failure,the system will throw a exception, so
you may use try..catch to catch this exception.


which particular exception should i catch ?
can you give me an example using some pseudo code

regards
shyam

Apr 11 '06 #3

P: n/a
shyam wrote:
HI

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.


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
Apr 11 '06 #4

P: n/a
In article <11**********************@z34g2000cwc.googlegroups .com>,
"shyam" <sh********@gmail.com> wrote:
HI

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


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.
Apr 11 '06 #5

P: n/a
In message <11**********************@g10g2000cwb.googlegroups .com>,
veldwolf <ve******@gmail.com> writes
hi,shyam,i think if write failure,the system will throw a exception, so
you may use try..catch to catch this exception.


Have you tested this?

What you "think" isn't helpful, particularly when it's wrong.

--
Richard Herring
Apr 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.