Topic: EOF... how does it work?
Author: firelark2@hotmail.com
Date: Mon, 12 Mar 2007 13:36:15 CST Raw View
Hi I'm wondering if anyone could explain to me, how the ios::eof()
function knows when the end of file has been reached? Is there a eof
symbol in each file like the end of string symbol "\0"? How does it
work? And does it work differently for binary files as opposed to
ascii files? I'm particularly interested to know how finding the eof
for binary files works.
Karl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: kuyper@wizard.net
Date: Mon, 12 Mar 2007 14:50:56 CST Raw View
firelark2@hotmail.com wrote:
> Hi I'm wondering if anyone could explain to me, how the ios::eof()
> function knows when the end of file has been reached? Is there a eof
> symbol in each file like the end of string symbol "\0"? How does it
> work? And does it work differently for binary files as opposed to
> ascii files? I'm particularly interested to know how finding the eof
> for binary files works.
It detects end of file in much the same way as the C macro feof(),
though I don't know if that fact helps you much. That means that It's
different for different implementations; it generally depends upon the
operating system used. As such, the answer to your question is outside
of the scope of the C++ standard, and therefore off-topic in this
newsgroup. If you want to find out how a particular implementation of
ios::eof() works, you can try checking the implementation's
documentation. Failing that, try asking this question on a newsgroup
devoted to that particular implementation, or that particular platform.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 12 Mar 2007 20:11:19 GMT Raw View
firelark2@hotmail.com wrote:
> Hi I'm wondering if anyone could explain to me, how the ios::eof()
> function knows when the end of file has been reached?
It has tried to read characters through a system call
and the call has returned zero characters.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 12 Mar 2007 22:01:06 GMT Raw View
kuyper@wizard.net wrote:
> It detects end of file in much the same way as the C macro feof()
The most important thing to know is that eof is after the fact,
not anticipatory, much like the temperature of a craps table.
Eof will tell you whether an attempt has been made to read past
the end of available data, not whether the next read will fail
due to lack of data. Never write code that looks like
while (!eof()) { read_some_more(); }
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "James Kanze" <james.kanze@gmail.com>
Date: Tue, 13 Mar 2007 08:48:28 CST Raw View
On Mar 12, 8:36 pm, firela...@hotmail.com wrote:
> Hi I'm wondering if anyone could explain to me, how the ios::eof()
> function knows when the end of file has been reached?
In a certain sense, it doesn't. All it knows is whether the
eofbit has been set or not (and there's nothing to prevent a
user defined << or >> to set it arbitrarily).
The convention, of course (followed by every standard << and >>,
and by correctly written user operators as well) is to set it
whenever you attempt to read, and there are no more characters
available. In istream and ostream, this is whenever overflow or
underflow return EOF. Which in turn depends entirely on the
streambuf itself.
> Is there a eof
> symbol in each file like the end of string symbol "\0"? How does it
> work? And does it work differently for binary files as opposed to
> ascii files? I'm particularly interested to know how finding the eof
> for binary files works.
How it works for files is implemented in filebuf, but almost
always, it is the result of a system read returning 0, or a
system write returning less than the number bytes written. As
for the system... it depends on the system; most modern systems
maintain the length of the file somewhere, and use this (for
disk files, of course... if you're reading from the keyboard, or
from a socket or pipe, different rules apply).
Note that in istream, the bit is normally only set when an
attempted read fails. But this refers to internal attempted
reads. The result is that eofbit may be unset when there is no
data left to be read, because no attempt has been made to read
it, or set, even though the last read succeeded, because the
last read required look-ahead to tell it when to stop, and that
look-ahead found the end of the file. The result is that you
practically never use ios::eof(), and never before failure. (If
a read fails, and ios::eof() and ios::bad() are both false, you
know that it is because of a format error in the input.
Regretfully, is ios::eof() is true, you can't always be 100%
certain that there was no format error.) The standard read
idiom is thus something like:
while ( input >> data ) {
// process data...
}
if ( input.bad() ) {
// serious error (except that a lot of implementations
// never generate this case).
} else if ( ! input.eof() ) {
// format error in the input file; e.g. we tried to
// read an int, and only found "abc".
} else {
// probably OK, but this can mask a few more exotic
// format errors as well.
}
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]