Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream file error checks?

Reply
Thread Tools

ofstream file error checks?

 
 
steve
Guest
Posts: n/a
 
      08-19-2003

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.
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-19-2003
steve <(E-Mail Removed)> wrote in message
news:bhtj79$(E-Mail Removed)...
>
> 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



 
Reply With Quote
 
 
 
 
Raoul Gough
Guest
Posts: n/a
 
      08-20-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (steve) writes:

> Thanks for the response.
> Below is the sample program I compiled with
> CC on Unix(Solaris 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: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]
--
Raoul Gough
"Let there be one measure for wine throughout our kingdom, and one
measure for ale, and one measure for corn" - Magna Carta
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream Error: Failed to write file: Result is too large. sharat C++ 1 04-16-2012 09:57 PM
ofstream class -- error-checking pauldepstein@att.net C++ 2 10-23-2005 05:28 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
ofstream error conditions jois.de.vivre@gmail.com C++ 3 06-03-2005 12:42 AM
Error using ofstream close() Colum C++ 2 12-19-2003 12:43 PM



Advertisments