473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to check write failure in ofstream??

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
5 14609
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
2
by: nt | last post by:
I am having a problem with a regular backup of an SQL Server (MSDE 2000) database to a local drive. I initiate the backup once a week, by issuing the required T-SQL, via ADO. In this case, the...
3
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++...
5
by: Roy Gourgi | last post by:
Hi, How can I write to a file and then be able to read what I wrote to it later on. I would like to save some information on to a file continuously and then be able to retrieve that information...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
6
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post ...
2
by: -Lost | last post by:
Is it possible to check if a file was not included properly? For example, I am using appendChild to insert a CSS document into the HEAD of my HTML document. I tested it with a non-existent CSS...
4
by: PH.D.Condidater.Li.Ning | last post by:
Hi, all, I am using an uninitialised variable that generating a 'Run-Time Check Failure #3' when the debug version is running. The code is complex and changing it would be risky, so how do I...
4
by: rince | last post by:
Hi! I have a complex class like class Complex { double re; double im; public: Complex();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.