ofstream file error checks?

Discussion in 'C++' started by steve, Aug 19, 2003.

  1. steve

    steve Guest

    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.
    steve, Aug 19, 2003
    #1
    1. Advertising

  2. steve

    Mike Wahler Guest

    steve <> wrote in message
    news:bhtj79$...
    >
    > 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
    Mike Wahler, Aug 19, 2003
    #2
    1. Advertising

  3. steve

    Raoul Gough Guest

    (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::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]
    --
    Raoul Gough
    "Let there be one measure for wine throughout our kingdom, and one
    measure for ale, and one measure for corn" - Magna Carta
    Raoul Gough, Aug 20, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    589
    Ivan Johansen
    Jul 14, 2005
  2. GGarramuno

    File checks....

    GGarramuno, Jan 6, 2004, in forum: Ruby
    Replies:
    4
    Views:
    88
    Gavin Sinclair
    Jan 7, 2004
  3. Martin DeMello
    Replies:
    1
    Views:
    78
    Sylvain Joyeux
    Feb 25, 2008
  4. froil
    Replies:
    4
    Views:
    111
    J├╝rgen Exner
    Mar 8, 2006
  5. sharat
    Replies:
    1
    Views:
    795
    Jens Thoms Toerring
    Apr 16, 2012
Loading...

Share This Page