Gunnar said:
Hello, I've just written a CPP program that reads integers from a
binary file, and used this code
while (my_ifstram.read( (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?
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:
http://www.dinkumware.com/libraries_ref.html
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
http://webstore.ansi.org
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:
http://anubis.dkuug.dk/jtc1/sc22/wg21/
Then there are the various FAQs for the C/C++ language newsgroups, e.g.,
; comp.lang.c++
http://www.parashift.com/c++-faq-lite/
; comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
; alt.comp.lang.learn.c-c++
http://www.snurse-l.org/acllc-c++/faq.html
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.read( (char* ) &number, sizeof(int)) { ... }
is essentially the same as this:
while ( ! my_ifstram.read( (char* ) &number, sizeof(int)).fail() )
{ ... }
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.read(...)
// 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.read(...)
// 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.