Getting Error Text from ifstream

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

    Getting Error Text from ifstream

    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_FAILU RE);
    }

    translating that somewhat to C++ yields me with

    const char *foobar = "this.txt";
    std::ifstream f(foobar);
    if (!f) {
    throw GenericExceptio n("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ästerli ch. Ich vermute, dass diese falschen Theorien genau
    deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
    HJP in de.sci.mathemat ik <4740ad67$0$381 1$5402220f@news .sunrise.ch>
  • David Harmon

    #2
    Re: Getting Error Text from ifstream

    On Tue, 20 Nov 2007 14:45:24 +0100 in comp.lang.c++, Johannes Bauer
    <dfnsonfsduifb@ gmx.dewrote,
    >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.

    Comment

    • Johannes Bauer

      #3
      Re: Getting Error Text from ifstream

      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ästerli ch. Ich vermute, dass diese falschen Theorien genau
      deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
      HJP in de.sci.mathemat ik <4740ad67$0$381 1$5402220f@news .sunrise.ch>

      Comment

      • David Harmon

        #4
        Re: Getting Error Text from ifstream

        On Tue, 20 Nov 2007 18:23:35 +0100 in comp.lang.c++, Johannes Bauer
        <dfnsonfsduifb@ gmx.dewrote,
        >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.

        Comment

        • James Kanze

          #5
          Re: Getting Error Text from ifstream

          On Nov 20, 2:45 pm, Johannes Bauer <dfnsonfsdu...@ gmx.dewrote:
          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_FAILU RE);
          }
          translating that somewhat to C++ yields me with
          const char *foobar = "this.txt";
          std::ifstream f(foobar);
          if (!f) {
          throw GenericExceptio n("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:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Johannes Bauer

            #6
            Re: Getting Error Text from ifstream

            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ästerli ch. Ich vermute, dass diese falschen Theorien genau
            deshalb so geliebt werden." -- Prophet und Visionär Hans Joss aka
            HJP in de.sci.mathemat ik <4740ad67$0$381 1$5402220f@news .sunrise.ch>

            Comment

            Working...