Getting Error Text from ifstream

Discussion in 'C++' started by Johannes Bauer, Nov 20, 2007.

  1. Hello group,

    coming from C, I'm used to somthing like this when opening a file

    const char *foobar = "this.txt";
    FILE *f;
    f = fopen(foobar, "r");
    if (!f) {
    fprintf(stderr, "Couldn't open %s: %s\n", foobar, strerror(errno));
    exit(EXIT_FAILURE);
    }

    translating that somewhat to C++ yields me with

    const char *foobar = "this.txt";
    std::ifstream f(foobar);
    if (!f) {
    throw GenericException("Couldn't open file.");
    }

    So I can detect that opening the file wasn't successful, but I don't
    know *why* (i.e. permissions, no such file or directory, etc.). My guess
    is that strerror and errno still do what I expect them to do, however I
    think that would be a very C-way to solve things.

    How can I accomplish strerror(errno) on the std::ifstream with C++ means?

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Nov 20, 2007
    #1
    1. Advertising

  2. Johannes Bauer

    David Harmon Guest

    On Tue, 20 Nov 2007 14:45:24 +0100 in comp.lang.c++, Johannes Bauer
    <> wrote,
    >So I can detect that opening the file wasn't successful, but I don't
    >know *why* (i.e. permissions, no such file or directory, etc.). My guess
    >is that strerror and errno still do what I expect them to do, however I
    >think that would be a very C-way to solve things.


    Use strerror(), perror(), and errno and consider yourself lucky. The
    C++ standard doesn't exactly promise that they will work for iostreams,
    but they do for the compilers I use. That's the best you get for now.
     
    David Harmon, Nov 20, 2007
    #2
    1. Advertising

  3. David Harmon schrieb:

    > Use strerror(), perror(), and errno and consider yourself lucky. The
    > C++ standard doesn't exactly promise that they will work for iostreams,
    > but they do for the compilers I use. That's the best you get for now.


    Oh, that's really a shame. Is anything planned about this in future
    specifications?

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Nov 20, 2007
    #3
  4. Johannes Bauer

    David Harmon Guest

    On Tue, 20 Nov 2007 18:23:35 +0100 in comp.lang.c++, Johannes Bauer
    <> wrote,
    >David Harmon schrieb:
    >
    >> Use strerror(), perror(), and errno and consider yourself lucky. The
    >> C++ standard doesn't exactly promise that they will work for iostreams,
    >> but they do for the compilers I use. That's the best you get for now.

    >
    >Oh, that's really a shame. Is anything planned about this in future
    >specifications?


    I have not heard of any. That would be a good question for
    comp.std.c++, where the c++ standardization process is discussed.
     
    David Harmon, Nov 20, 2007
    #4
  5. Johannes Bauer

    James Kanze Guest

    On Nov 20, 2:45 pm, Johannes Bauer <> wrote:
    > coming from C, I'm used to somthing like this when opening a file


    > const char *foobar = "this.txt";
    > FILE *f;
    > f = fopen(foobar, "r");
    > if (!f) {
    > fprintf(stderr, "Couldn't open %s: %s\n", foobar, strerror(errno));
    > exit(EXIT_FAILURE);
    > }


    > translating that somewhat to C++ yields me with


    > const char *foobar = "this.txt";
    > std::ifstream f(foobar);
    > if (!f) {
    > throw GenericException("Couldn't open file.");
    > }


    > So I can detect that opening the file wasn't successful, but I
    > don't know *why* (i.e. permissions, no such file or directory,
    > etc.). My guess is that strerror and errno still do what I
    > expect them to do, however I think that would be a very C-way
    > to solve things.


    It's not really a C way either, since C doesn't say anything
    about the state of errno after a failed fopen.

    Pragmatically, it's what I do as well.

    > How can I accomplish strerror(errno) on the std::ifstream with
    > C++ means?


    You can't associate errno with an std::ifstream anymore than you
    can associate it a FILE*.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 20, 2007
    #5
  6. James Kanze schrieb:

    > It's not really a C way either, since C doesn't say anything
    > about the state of errno after a failed fopen.
    >
    > Pragmatically, it's what I do as well.


    Does it really not? My Linux manpage says its fopen implementation is in
    accordance with ANSI C3.159-1989 ("ANSI C") and errno (according to
    another manpage) seems to appear in the ISO-C standard - yet the
    connection of the two is purely a Linux thing and could break on any
    other system?

    >> How can I accomplish strerror(errno) on the std::ifstream with
    >> C++ means?

    >
    > You can't associate errno with an std::ifstream anymore than you
    > can associate it a FILE*.


    This is really a lack, as I think the language should provide means for
    that. Many are, in fact, operating system independent after all.

    Greetings,
    Johannes

    --
    "Viele der Theorien der Mathematiker sind falsch und klar
    Gotteslästerlich. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathematik <4740ad67$0$3811$>
     
    Johannes Bauer, Nov 20, 2007
    #6
  7. Johannes Bauer

    James Kanze Guest

    On Nov 21, 12:12 am, Johannes Bauer <> wrote:
    > James Kanze schrieb:


    > > It's not really a C way either, since C doesn't say anything
    > > about the state of errno after a failed fopen.


    > > Pragmatically, it's what I do as well.


    > Does it really not? My Linux manpage says its fopen
    > implementation is in accordance with ANSI C3.159-1989 ("ANSI
    > C") and errno (according to another manpage) seems to appear
    > in the ISO-C standard - yet the connection of the two is
    > purely a Linux thing and could break on any other system?


    In theory. All ISO 9899 says is that "If the open operation
    fails, fopen returns a null pointer." In addition, look at the
    possible values for errno on the Linux (or any other Unix
    system) man page: the only values defined by the C standard are
    EDOM, EILSEQ and ERANGE. (I'm pretty sure that Posix requires a
    good deal more.)

    In practice, errno was put into the C standard for a reason.
    The C standard also clearly says (concerning errno.h):
    "Additional macro definitions, beginning with E and a digit or E
    and an uppercase letter, may also be specified by the
    implementation." It also says: "The value of errno is zero at
    program startup, but is never set to zero by any library
    function. The value of errno may be set to nonzero by a library
    function call whether or not there is an error, provided the use
    of errno is not documented in the description of the function in
    this International Standard." From a QoI point of view, I
    expect errno to be set to something significant in case of an
    error---that's what it's there for. (I don't count on it not
    being set in the absense of an error. I can't think of a good
    example from the standard, but imagine a function like
    gethostname(), which uses several different strategies. Some of
    which may have failed and set errno before the function finally
    finds one which works.)

    The problem in C++, of course, is that this is all
    implementation dependent, and the implementation (Linux, Posix,
    etc.) only documents the C functions. Again, in practice,
    neither fopen nor filebuf::eek:pen actually set errno themselves.
    Both of them ultimately call the Posix function open(), and the
    Posix function open() does set errno in case of an error. And
    I'd be very surprised (and disappointed) that an implementation
    then modified it if open() failed.

    > >> How can I accomplish strerror(errno) on the std::ifstream with
    > >> C++ means?


    > > You can't associate errno with an std::ifstream anymore than you
    > > can associate it a FILE*.


    > This is really a lack, as I think the language should provide
    > means for that. Many are, in fact, operating system
    > independent after all.


    And others aren't. Only defining some, but leaving the
    implementation free to use others as well, doesn't buy you much.

    It would make sense if the standard said something to the effect
    that in case of failure, errno would be set to an implementation
    defined non-zero value, depending on the cause of the failure.
    But I wouldn't worry about it too much, since that's the obvious
    intention, and what all implementations do anyway. Far more
    important would be cleaning up the error handling in general, so
    that e.g. you could reliably distinguish between a hardware read
    error, an error in the format, and the end of file.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 21, 2007
    #7
    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. wtnt
    Replies:
    1
    Views:
    3,038
    Jonathan Turkanis
    Jan 30, 2004
  2. Francis Bell
    Replies:
    5
    Views:
    414
    Francis Bell
    May 24, 2004
  3. Mastupristi
    Replies:
    1
    Views:
    389
    Jonathan Mcdougall
    Nov 9, 2005
  4. toton
    Replies:
    3
    Views:
    397
    Ondra Holub
    Jan 8, 2007
  5. Vadim Volevach

    ifstream in text mode + seekg problem

    Vadim Volevach, Jan 18, 2008, in forum: C++
    Replies:
    1
    Views:
    453
    James Kanze
    Jan 18, 2008
Loading...

Share This Page