Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to check write failure in ofstream??

6 views
Skip to first unread message

shyam

unread,
Apr 11, 2006, 12:04:12 AM4/11/06
to
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

veldwolf

unread,
Apr 11, 2006, 1:57:37 AM4/11/06
to
hi,shyam,i think if write failure,the system will throw a exception, so
you may use try..catch to catch this exception.

shyam

unread,
Apr 11, 2006, 4:32:10 AM4/11/06
to

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

Tom Widmer

unread,
Apr 11, 2006, 7:17:08 AM4/11/06
to
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

Daniel T.

unread,
Apr 11, 2006, 7:26:35 AM4/11/06
to
In article <1144728252.1...@z34g2000cwc.googlegroups.com>,
"shyam" <shyam...@gmail.com> wrote:

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.

Richard Herring

unread,
Apr 11, 2006, 12:45:41 PM4/11/06
to
In message <1144735057.3...@g10g2000cwb.googlegroups.com>,
veldwolf <veld...@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

0 new messages