how to check write failure in ofstream??

S

shyam

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
 
V

veldwolf

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

shyam

veldwolf said:
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
 
T

Tom Widmer

shyam said:
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
 
D

Daniel T.

"shyam said:
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.
 
R

Richard Herring

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top