Does the open operation succeed?

Discussion in 'C++' started by fl, Jan 4, 2013.

  1. fl

    fl Guest

    Hi,

    Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).

    Could you tell me how to check "in" stream?
    Thanks,


    ................
    #include "io_preamble.h"

    // opens in binding it to the given file
    ifstream& open_file(ifstream &in, const string &file)
    {
    in.close(); // close in case it was already open
    in.clear(); // clear any existing errors

    // if the open fails, the stream will be in an invalid state
    in.open(file.c_str()); // open the file we were given

    return in; // condition state is good if open succeeded
    }
     
    fl, Jan 4, 2013
    #1
    1. Advertising

  2. On 1/4/2013 3:51 PM, fl wrote:
    > Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).
    >
    > Could you tell me how to check "in" stream?


    try 'in.good()'.

    > Thanks,
    >
    >
    > ...............
    > #include "io_preamble.h"
    >
    > // opens in binding it to the given file
    > ifstream& open_file(ifstream &in, const string &file)
    > {
    > in.close(); // close in case it was already open
    > in.clear(); // clear any existing errors
    >
    > // if the open fails, the stream will be in an invalid state
    > in.open(file.c_str()); // open the file we were given
    >
    > return in; // condition state is good if open succeeded
    > }
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 4, 2013
    #2
    1. Advertising

  3. fl

    Bill Gill Guest

    On 1/4/2013 2:51 PM, fl wrote:
    > Hi,
    >
    > Below the dot line is a function except from C++ primer, 4th edition. When I use this function, I suspect the open operation fails. I do not know how to check the "in" stream status. The curious thing is that it mentioned open state in the comment part below, but there is nothing to do that (at least I do not find it).
    >
    > Could you tell me how to check "in" stream?
    > Thanks,
    >
    >
    > ...............
    > #include "io_preamble.h"
    >
    > // opens in binding it to the given file
    > ifstream& open_file(ifstream &in, const string &file)
    > {
    > in.close(); // close in case it was already open
    > in.clear(); // clear any existing errors
    >
    > // if the open fails, the stream will be in an invalid state
    > in.open(file.c_str()); // open the file we were given
    >
    > return in; // condition state is good if open succeeded
    > }
    >

    It looks to me as though you need to check the value returned
    by open_file to see if the open worked.

    if(!openfile(in, filename))
    .... // recovery code

    Bill Gill
     
    Bill Gill, Jan 5, 2013
    #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,147
    bcoulson
    Aug 31, 2009
  2. Anonymousgoogledeja

    how can i Know whether atoi function call succeed?

    Anonymousgoogledeja, Aug 22, 2005, in forum: C Programming
    Replies:
    15
    Views:
    1,003
    Jack Klein
    Aug 23, 2005
  3. david ullua
    Replies:
    13
    Views:
    689
  4. dotnet
    Replies:
    0
    Views:
    356
    dotnet
    Aug 21, 2009
  5. J Krugman

    Tests fail in cpan, succeed manually

    J Krugman, Apr 10, 2004, in forum: Perl Misc
    Replies:
    8
    Views:
    128
    James Willmore
    Apr 26, 2004
Loading...

Share This Page