Does ifstream know why it is unable to open file?

Discussion in 'C++' started by Alex Vinokur, Aug 13, 2003.

  1. Alex Vinokur

    Alex Vinokur Guest

    -------------------------------
    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:
    http://mathforum.org/library/view/10978.html
    =====================================
    Alex Vinokur, Aug 13, 2003
    #1
    1. Advertising

  2. "Alex Vinokur" <> writes:

    > -------------------------------
    > 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?


    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
    Frank Schmitt, Aug 20, 2003
    #2
    1. Advertising

  3. Frank Schmitt wrote:
    > "Alex Vinokur" <> writes:
    >
    >>Can ifstream know why it is unable to open the file?

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


    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.
    Kevin Goodsell, 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. Replies:
    1
    Views:
    1,419
  2. Charlie
    Replies:
    4
    Views:
    9,749
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    222
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page