Error reason for fstream failures

Discussion in 'C++' started by Steve Keller, Feb 5, 2013.

  1. Steve Keller

    Steve Keller Guest

    How can I get the reason for a file operation failure, say after
    fstream::eek:pen() or fstream::read()?

    In C, after fopen(), fread(), etc. you can check errno or get a
    message using perror() and strerror().

    I cannot find any equivalent for C++ fstreams.
     
    Steve Keller, Feb 5, 2013
    #1
    1. Advertising

  2. Steve Keller

    Melzzzzz Guest

    On Tue, 05 Feb 2013 15:11:28 +0100, Steve Keller wrote:

    > How can I get the reason for a file operation failure, say after
    > fstream::eek:pen() or fstream::read()?
    >
    > In C, after fopen(), fread(), etc. you can check errno or get a message
    > using perror() and strerror().
    >
    > I cannot find any equivalent for C++ fstreams.


    This is why my college said that streams are great evil ;)
    On Linux one can still chek errno after stream failure.
     
    Melzzzzz, Feb 5, 2013
    #2
    1. Advertising

  3. Steve Keller

    Nobody Guest

    On Tue, 05 Feb 2013 15:11:28 +0100, Steve Keller wrote:

    > How can I get the reason for a file operation failure, say after
    > fstream::eek:pen() or fstream::read()?
    >
    > In C, after fopen(), fread(), etc. you can check errno or get a message
    > using perror() and strerror().


    Nothing in the C standards require fopen() or fread() to set errno on
    failure. More generally, the standards only define the values EDOM, EILSEQ
    and ERANGE, so anything else is implementation-dependent.

    > I cannot find any equivalent for C++ fstreams.


    fstream::eek:pen() calls fstream::rdbuf()->open(), which operates "as if by
    calling std::fopen()" (27.9.1.4[filebuf.members]p2).

    In C++11, <cerrno> is aligned with POSIX rather than C, so EEXIST etc are
    defined. However, fopen() is defined by reference to the C standard,
    which doesn't specify its behaviour on failure with respect to errno (see
    above). And POSIX doesn't mention C++.

    So I guess it all depends upon what you want to read into "as if by
    std::fopen()".
     
    Nobody, Feb 6, 2013
    #3
  4. On 05.02.13 15.12, Melzzzzz wrote:
    > This is why my college said that streams are great evil ;)


    Full ACK. I know about C++ streams about 20 years. And in all these 20
    years of C++ programming I did not find even one use case where fstreams
    fit the need best. I tested them from time to time, but it never made it
    to production code.
    Either I ended up with the C style I/O API (fopen...) or I directly
    called the platform specific operating system functions. Mostly I took
    the first choice. Only if the code is bound to one platform anyway I
    used the OS APIs.

    > On Linux one can still chek errno after stream failure.


    Yes, but the code is no longer portable.


    Marcel.
     
    Marcel Müller, Feb 6, 2013
    #4
  5. Steve Keller

    Jorgen Grahn Guest

    On Wed, 2013-02-06, Marcel Müller wrote:
    > On 05.02.13 15.12, Melzzzzz wrote:
    >> This is why my college said that streams are great evil ;)

    >
    > Full ACK. I know about C++ streams about 20 years. And in all these 20
    > years of C++ programming I did not find even one use case where fstreams
    > fit the need best. I tested them from time to time, but it never made it
    > to production code.


    What was wrong with it, all those times? Reading a line of text into
    a std::string is one simple and very useful thing. Being able to print
    a user-defined type is another.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Feb 6, 2013
    #5
  6. Steve Keller <> writes:

    > How can I get the reason for a file operation failure, say after
    > fstream::eek:pen() or fstream::read()?


    In C++11, if you enable exceptions for the stream, then you can
    get an error code from std::ios_base::failure, which is derived
    from std::system_error.
     
    Kalle Olavi Niemitalo, Feb 6, 2013
    #6
    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. Kevin Spencer

    Re: So many failures early on

    Kevin Spencer, Aug 13, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    356
    Kevin Spencer
    Aug 27, 2003
  2. Guest
    Replies:
    0
    Views:
    434
    Guest
    Jun 19, 2004
  3. tommy
    Replies:
    0
    Views:
    387
    tommy
    Jul 19, 2004
  4. jlambrecht
    Replies:
    0
    Views:
    587
    jlambrecht
    Dec 22, 2004
  5. Armando
    Replies:
    6
    Views:
    779
    Armando
    Jan 29, 2004
Loading...

Share This Page