ofstream file error checks?

S

steve

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.
 
M

Mike Wahler

steve said:
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
 
R

Raoul Gough

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::eek:ut|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]
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top