ofstream file error checks?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    ofstream file error checks?


    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.
  • Mike Wahler

    #2
    Re: ofstream file error checks?

    steve <steveu@w-steveu.ih.lucen t.com> wrote in message
    news:bhtj79$irj @netnews.proxy. lucent.com...[color=blue]
    >
    > Is there a way to catch file errors while writing
    > to a file using ofstream?[/color]

    Yes. Depending upon which functions you use,
    either check the function return value, or
    check the stream state.
    [color=blue]
    > For ex., if the file is deleted or permissions changed
    > by another process after it is opened,[/color]

    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. :-)
    [color=blue]
    > or when the disk is full.[/color]

    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
    [color=blue]
    >
    > 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.[/color]

    Show us the code.
    [color=blue]
    >
    > Regarding the usage, I'm opening the file using
    > ofstream(....) and then using "<<" to do the writes.
    > Thanks for any help.[/color]

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

    -Mike



    Comment

    • Raoul Gough

      #3
      Re: ofstream file error checks?

      steveu@w-steveu.ih.lucen t.com (steve) writes:
      [color=blue]
      > 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.[/color]

      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.progr ammer for that, since it goes beyond what C++
      provides).
      [color=blue]
      > Tried similar scenario
      > where disk is full. Same result! Am I missing something?[/color]

      Don't know about the disk full error - did you try flushing or closing
      the file before assuming that the write succeeded?
      [color=blue]
      > =============== =============== =============== ===
      > #include <string>
      > #include <fstream>
      > #include <iostream>
      >
      > using namespace std;
      >
      > main(int , char **)
      > {
      > string s1 = "MyFile";
      > ofstream out(s1.c_str(), ios::out|ios::a pp);
      > 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();[/color]

      Try searching for some of James Kanze's articles in
      comp.lang.c++.m oderated, 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

      Comment

      Working...