std::fstream

Discussion in 'C++' started by John Ratliff, Sep 1, 2005.

  1. John Ratliff

    John Ratliff Guest

    Is this a legitimate (standards compliant) way to check for errors in
    file I/O.

    #include <fstream>

    fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

    if (output) {
    // we're good to write to file.ext in binary mode

    output.close();
    }

    I thought I saw a C++ book say this was good, but I can't find a
    reference on the internet and I don't know where my C++ book is right now.

    I've seen this:

    if (!output) {
    // file wasn't opened
    }

    Also, Is binary default or do I need to specify it explicitly when I
    open the fstream?

    Thanks,

    --John Ratliff
     
    John Ratliff, Sep 1, 2005
    #1
    1. Advertising

  2. John Ratliff wrote:
    > Is this a legitimate (standards compliant) way to check for errors in
    > file I/O.
    >
    > #include <fstream>
    >
    > fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);
    >
    > if (output) {
    > // we're good to write to file.ext in binary mode
    >
    > output.close();
    > }
    >
    > I thought I saw a C++ book say this was good, but I can't find a
    > reference on the internet and I don't know where my C++ book is right now.
    >
    > I've seen this:
    >
    > if (!output) {
    > // file wasn't opened
    > }
    >
    > Also, Is binary default or do I need to specify it explicitly when I
    > open the fstream?


    Every stream object allows you to call 'good()', 'fail()', and 'eof()',
    which are considered enough to check for error conditions. More in your
    book when you find it.

    V
     
    Victor Bazarov, Sep 1, 2005
    #2
    1. Advertising

  3. John Ratliff

    John Ratliff Guest

    Victor Bazarov wrote:
    > Every stream object allows you to call 'good()', 'fail()', and 'eof()',
    > which are considered enough to check for error conditions. More in your
    > book when you find it.


    My book is packed in a box until I move, so that won't be for awhile.

    What exactly does if (output) mean here?

    I know !output calls fail.

    --John Ratliff
     
    John Ratliff, Sep 1, 2005
    #3
  4. John Ratliff

    mlimber Guest

    John Ratliff wrote:
    > Is this a legitimate (standards compliant) way to check for errors in
    > file I/O.
    >
    > #include <fstream>
    >
    > fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);


    std::fstream output("file.ext",
    std::ios_base::eek:ut | std::ios_base::binary);

    >
    > if (output) {
    > // we're good to write to file.ext in binary mode
    >
    > output.close();
    > }
    >
    > I thought I saw a C++ book say this was good, but I can't find a
    > reference on the internet and I don't know where my C++ book is right now.


    That'll make sure the file is opened, but other errors can occur along
    the way. Also note that close() is called automatically by the
    destructor if that's convenient for you. Try this reference:

    http://www.cplusplus.com/ref/iostream/fstream/

    The if statement's condition implicitly invokes io_base::eek:perator
    void*() const, which basically returns 0 if fail() would return true or
    non-zero if it wouldn't.

    [snip]
    > Also, Is binary default or do I need to specify it explicitly when I
    > open the fstream?


    It is not the default; text is.

    Cheers! --M
     
    mlimber, Sep 1, 2005
    #4
  5. John Ratliff

    John Ratliff Guest

    mlimber wrote:
    > John Ratliff wrote:
    >
    >>Is this a legitimate (standards compliant) way to check for errors in
    >>file I/O.
    >>
    >>#include <fstream>
    >>
    >>fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

    >
    >
    > std::fstream output("file.ext",
    > std::ios_base::eek:ut | std::ios_base::binary);
    >


    Heh heh. Yeah, forgot the std:: qualifier.

    >
    >>if (output) {
    >> // we're good to write to file.ext in binary mode
    >>
    >> output.close();
    >>}
    >>
    >>I thought I saw a C++ book say this was good, but I can't find a
    >>reference on the internet and I don't know where my C++ book is right now.

    >
    >
    > That'll make sure the file is opened, but other errors can occur along
    > the way. Also note that close() is called automatically by the
    > destructor if that's convenient for you. Try this reference:
    >
    > http://www.cplusplus.com/ref/iostream/fstream/
    >
    > The if statement's condition implicitly invokes io_base::eek:perator
    > void*() const, which basically returns 0 if fail() would return true or
    > non-zero if it wouldn't.


    I thought it might be that, but I wasn't sure how it would do that
    without an explicit cast.

    >
    > [snip]
    >
    >>Also, Is binary default or do I need to specify it explicitly when I
    >>open the fstream?

    >
    >
    > It is not the default; text is.


    Thanks,

    --John Ratliff
     
    John Ratliff, Sep 1, 2005
    #5
  6. John Ratliff

    mlimber Guest

    John Ratliff wrote:
    [snip]
    > >>fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

    > >
    > > std::fstream output("file.ext",
    > > std::ios_base::eek:ut | std::ios_base::binary);

    >
    > Heh heh. Yeah, forgot the std:: qualifier.


    And the | operator. :)

    > >>if (output) {
    > >> // we're good to write to file.ext in binary mode
    > >>
    > >> output.close();
    > >>}
    > >>
    > >>I thought I saw a C++ book say this was good, but I can't find a
    > >>reference on the internet and I don't know where my C++ book is right now.

    > >
    > > That'll make sure the file is opened, but other errors can occur along
    > > the way. Also note that close() is called automatically by the
    > > destructor if that's convenient for you. Try this reference:
    > >
    > > http://www.cplusplus.com/ref/iostream/fstream/
    > >
    > > The if statement's condition implicitly invokes io_base::eek:perator
    > > void*() const, which basically returns 0 if fail() would return true or
    > > non-zero if it wouldn't.

    >
    > I thought it might be that, but I wasn't sure how it would do that
    > without an explicit cast.


    That's a valid conversion (thanks to the conversion operator io_base
    defines) and the best the compiler can find, so it takes it. (BTW, I
    was looking at the code for STL-port, and it has a comment that says
    the conversion operator is actually required in std::basic_ios, not
    std::ios_base. STL-port defines it in the latter to eliminate
    unnecessary code duplication from template instantiation.)

    Cheers! --M
     
    mlimber, Sep 1, 2005
    #6
  7. John Ratliff wrote:
    > Victor Bazarov wrote:
    >
    >> Every stream object allows you to call 'good()', 'fail()', and 'eof()',
    >> which are considered enough to check for error conditions. More in your
    >> book when you find it.

    >
    >
    > My book is packed in a box until I move, so that won't be for awhile.
    >
    > What exactly does if (output) mean here?
    >
    > I know !output calls fail.


    std::basic_ios has a conversion to 'void*' defined. That means you can
    use it when a logical expression is expected. The pointer it returns is
    non-null if the state is '!fail()'.

    if (output)

    is the same as

    if (output.operator void*() != NULL)

    or, semantically is the same as

    if (!output.fail())

    V
     
    Victor Bazarov, Sep 1, 2005
    #7
  8. John Ratliff wrote:
    > Victor Bazarov wrote:
    > > Every stream object allows you to call 'good()', 'fail()', and 'eof()',
    > > which are considered enough to check for error conditions. More in your
    > > book when you find it.

    >
    > My book is packed in a box until I move, so that won't be for awhile.
    >
    > What exactly does if (output) mean here?


    I see you haven't packed your PC, so why don't you just look in
    sources?

    GNU ISO C++ Library:

    template<typename _CharT, typename _Traits>
    class basic_ios : public ios_base
    {
    // ...

    //@{
    /**
    * @brief The quick-and-easy status check.
    *
    * This allows you to write constructs such as
    * "if (!a_stream) ..." and "while (a_stream) ..."
    */
    operator void*() const
    { return this->fail() ? 0 : const_cast<basic_ios*>(this); }

    bool
    operator!() const
    { return this->fail(); }
    //@}
    // ...
    };
     
    Maxim Yegorushkin, Sep 2, 2005
    #8
    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. Armando
    Replies:
    6
    Views:
    770
    Armando
    Jan 29, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,388
    Ivan Vecerina
    Mar 17, 2005
  3. DaVinci
    Replies:
    3
    Views:
    431
    Jacek Dziedzic
    Apr 9, 2006
  4. DaVinci
    Replies:
    7
    Views:
    730
    Fraser Ross
    Apr 7, 2006
  5. mathieu
    Replies:
    2
    Views:
    2,428
    mathieu
    Apr 8, 2009
Loading...

Share This Page