Does ifstream know why it is unable to open file?

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

    Does ifstream know why it is unable to open file?

    -------------------------------
    ifstream fin ("foo.txt");
    if (!fin) cout << "Warning: Unable to open file" << endl;
    -------------------------------

    There are several reasons for getting that warning :
    * foo.txt doesn't exist
    * foo.txt exists but is busy
    * etc.

    Can ifstream know why it is unable to open the file?

    =============== =============== =======
    Alex Vinokur
    mailto:alexvn@c onnect.to

    =============== =============== =======


  • Frank Schmitt

    #2
    Re: Does ifstream know why it is unable to open file?

    "Alex Vinokur" <alexvn@bigfoot .com> writes:
    [color=blue]
    > -------------------------------
    > ifstream fin ("foo.txt");
    > if (!fin) cout << "Warning: Unable to open file" << endl;
    > -------------------------------
    >
    > There are several reasons for getting that warning :
    > * foo.txt doesn't exist
    > * foo.txt exists but is busy
    > * etc.
    >
    > Can ifstream know why it is unable to open the file?[/color]

    If it knows, it's platform-dependent (as Victor already
    pointed out).
    If you don't care about portability, You might want to check
    if your implementation sets errno upon failure - some of them do.

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frank DOT schmitt AT 4sc DOT com

    Comment

    • Kevin Goodsell

      #3
      Re: Does ifstream know why it is unable to open file?

      Frank Schmitt wrote:[color=blue]
      > "Alex Vinokur" <alexvn@bigfoot .com> writes:
      >[color=green]
      >>Can ifstream know why it is unable to open the file?[/color]
      >
      >
      > If it knows, it's platform-dependent (as Victor already
      > pointed out).
      > If you don't care about portability, You might want to check
      > if your implementation sets errno upon failure - some of them do.
      >[/color]

      Why would checking errno require not caring about portability? errno is
      standard. Either the function will set it or not. It's easy to check and
      use it if it has a meaningful value.

      But I don't know how common it is for implementations to set errno in
      this case.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      Working...