ifstream.read manual

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

    ifstream.read manual

    Hello, I've just written a CPP program that reads integers from a
    binary file, and used this code
    while (my_ifstram.rea d( (char* ) &number, sizeof(int))
    {
    // do something with number
    }

    My question is now, where can I find a manual that describes what
    the read method does with the ifstream object?
    I'm sitting here with my Linux/Debian machine, but I have not found any
    manual for that in the system.

    The assumption that while returns something usefull as long as I can read
    the file seems reasonable, but I wouldn't mind having documentation that is
    off-line readable.
    Where can I find that?

    /G++
  • Jonathan Mcdougall

    #2
    Re: ifstream.read manual

    On Sun, 27 Jul 2003 22:25:14 GMT, Gunnar <gunnar@gunix.d k> wrote:
    [color=blue]
    >Hello, I've just written a CPP program that reads integers from a
    >binary file, and used this code
    >while (my_ifstram.rea d( (char* ) &number, sizeof(int))
    >{
    > // do something with number
    >}
    >
    >My question is now, where can I find a manual that describes what
    >the read method does with the ifstream object?[/color]

    The C++ Standard Library, by Josuttis. A must have.

    From it :

    istream& istream::read (char* str, streamsize count)

    - reads 'count' characters in the string 'str'
    - returns the stream. The stream's state tells whether the read was
    successful
    - the string in 'str' is _not_ terminate automaticcal with the string
    termination character
    - the caller must ensure that 'str' has sufficient space to store
    'count' characters
    - encountering end-of-file during reading is considered an error, and
    'failbit' is set (in addition to 'eofbit')


    Jonathan

    Comment

    • Jim Fischer

      #3
      Re: ifstream.read manual

      Gunnar wrote:[color=blue]
      > Hello, I've just written a CPP program that reads integers from a
      > binary file, and used this code
      > while (my_ifstram.rea d( (char* ) &number, sizeof(int))
      > {
      > // do something with number
      > }
      >
      > My question is now, where can I find a manual that describes what
      > the read method does with the ifstream object?
      > I'm sitting here with my Linux/Debian machine, but I have not found any
      > manual for that in the system.
      >
      > The assumption that while returns something usefull as long as I can read
      > the file seems reasonable, but I wouldn't mind having documentation that is
      > off-line readable.
      > Where can I find that?[/color]

      AFAIK, the GNU Compiler Collection [GCC] does not provide man pages for
      the standard C++ library (probably because the standard C++ library is
      just too huge, and it is probably going to get quite a bit bigger in the
      near future). So here's a standard C++ library reference that can be
      used online and/or purchased and downloaded for off-line use:



      If you're willing to spend $18(US), you can buy/download a copy of the
      actual ISO/IEC C++ standard in Adobe Acrobat (.pdf) format from



      Click on the "Standards Search" link (left-hand side of the page) and
      then search for '14882'. [n.b. Searching on 14882 returns two items. The
      $283 item is the bound, printed manual. The $18 item is the Adobe
      Acrobat file.] If you decide to buy this document, here is a web site
      that documents the proposed changes to the C++ standard since 1998:



      Then there are the various FAQs for the C/C++ language newsgroups, e.g.,

      ; comp.lang.c++


      ; comp.lang.c


      ; alt.comp.lang.l earn.c-c++


      And of course, there are all kinds of books out there that describe the
      C++ programming language in gory detail.


      FWIW, your while() statement,

      while (my_ifstram.rea d( (char* ) &number, sizeof(int)) { ... }

      is essentially the same as this:

      while ( ! my_ifstram.read ( (char* ) &number, sizeof(int)).fa il() )
      { ... }

      IOW, if the read attempt does not fail, then enter the body of the
      while() loop.

      (*) The .fail() method returns 'true' if the iostream object's 'failbit'
      and/or 'badbit' state flags are asserted; otherwise it returns 'false'.
      If an istream object fails to read the expected characters -- in this
      particular case, if less than sizeof(int) bytes are available for
      reading -- the istream object asserts its 'failbit' state flag. An
      istream object asserts its 'badbit' flag if there is a "loss of
      integrity" in the input sequence (whatever that might mean for a given
      system). Note that the .fail() method does NOT test for the end-of-file
      condition (i.e., it does not test whether the istream object's 'eofbit'
      state flag is asserted). This is because the program can potentially
      read some data from the input sequence and then detect the EOF condition:

      my_ifstream.rea d(...)
      // eofbit = true (end-of-file detected)
      // failbit = false (data was read successfully, so no failure)
      // badbit = false (no gophers chewed thru the fiber)

      Since some data was successfully read from the input stream just prior
      to detecting the EOF condition, the program should apparently enter the
      body of the while() loop again to process that last bit of data. So the
      istream's eofbit state flag does not -- and /should not/ -- play a role
      in determining whether the program enters the body of the while() loop.
      FWIW, on the very next iteration of the while() loop, the read attempt
      fails because there is no more data available:

      my_ifstream.rea d(...)
      // eofbit = true (end-of-file detected)
      // failbit = true (failed to read the expected data item)
      // badbit = false

      On this iteration, then, the program breaks out of the while() loop.

      --
      Jim

      To reply by email, remove "link" and change "now.here" to "yahoo"
      jfischer_link58 09{at}now.here. com


      Comment

      Working...