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

ofstream file error checks?

P: n/a

Is there a way to catch file errors while writing
to a file using ofstream?
For ex., if the file is deleted or permissions changed
by another process after it is opened,
or when the disk is full.

I couldn't figure out which members could be used
to check for these types of errors.
I tried the good(), bad(), fail() etc.,
after writing to a full disk, deleted file etc.
They all returned success always.

Regarding the usage, I'm opening the file using
ofstream(....) and then using "<<" to do the writes.
Thanks for any help.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
steve <st****@w-steveu.ih.lucent.com> wrote in message
news:bh********@netnews.proxy.lucent.com...

Is there a way to catch file errors while writing
to a file using ofstream?
Yes. Depending upon which functions you use,
either check the function return value, or
check the stream state.
For ex., if the file is deleted or permissions changed
by another process after it is opened,
If your operating system allows one process to delete
or modify such attributes of a file that is already open
by another process, I think you need a new operating system. :-)
or when the disk is full.
If a write operation fails for whatever reason,
the stream state willbe in a 'fail' state. Check for

stream.good() == false
or
stream.fail() == true
or
stream.bad() == true

I couldn't figure out which members could be used
to check for these types of errors.
I tried the good(), bad(), fail() etc.,
after writing to a full disk, deleted file etc.
They all returned success always.
Show us the code.

Regarding the usage, I'm opening the file using
ofstream(....) and then using "<<" to do the writes.
Thanks for any help.


Show us the code. Try to compose a small compilable
program that exhibits the problem behavior.

-Mike

Jul 19 '05 #2

P: n/a
st****@w-steveu.ih.lucent.com (steve) writes:
Thanks for the response.
Below is the sample program I compiled with
CC on Unix(Solaris8) Sun Ultra. Executed in one shell
window. While on the sleep line, from another shell
window, ran "rm MyFile". When done, file was removed
but got all "SUCCESS" outputs on screen, never got
to the "FAIL" line, no coredump.
As I mentioned in my other post, this is correct Unix behaviour.
However, there *are* o/s specific means of locking a file - maybe you
should look into them (but you'll need a newsgroup like
comp.unix.programmer for that, since it goes beyond what C++
provides).
Tried similar scenario
where disk is full. Same result! Am I missing something?
Don't know about the disk full error - did you try flushing or closing
the file before assuming that the write succeeded?
================================================
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

main(int , char **)
{
string s1 = "MyFile";
ofstream out(s1.c_str(), ios::out|ios::app);
if(out.good()) {
cout << "good() - SUCCESS " << endl;
} else {
cout << "good() - FAIL " << endl;
}
out << "File opened" << endl;

sleep(10); // Another Unix shell window execute "rm MyFile"

out << "Hello World" << endl;
out.flush();

if(out.good()) {
cout << "good() - SUCCESS " << endl;
} else {
cout << "good() - FAIL " << endl;
}
if(out.fail()) {
cout << "fail() - FAIL " << endl;
} else {
cout << "fail() - SUCCESS " << endl;
}
if(out.bad()) {
cout << "bad() - FAIL " << endl;
} else {
cout << "bad() - SUCCESS " << endl;
}

out.close();


Try searching for some of James Kanze's articles in
comp.lang.c++.moderated, about why you shouldn't rely on calling
close() from within destructors (if the close fails, what do you do
then?). Maybe the close is failing here?

[snip]
--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.