"ericunfuk" <xu***********@ gmail.comwrites :
I'm really confused!!!
Yes.
Can anyone answer this please:
Suppose now I fseek()ed, then I do fread() in which case I can tell
from the current file position pointer, it must encounter the original
end of the file(Let's assume this end-of-file position x).Would this
position x disappeared as I fseek()ed, as it says fseek() clears end-
of-file, so now if x has disappeared, in my understanding, fread()
would not encounter the original end-of-file it will read beyond x and
no error(even EOF) will happen?
There are several different entities covered by the terms "EOF" and/or
"end-of-file", and I think you're mixing them up.
1. EOF is a macro that expands to a constant integer expression. It's
the value returned by certain functions when they try and fail to
read data from a file. For example, fgetc() returns the value of
the next character in the file if that's possible; if it isn't, it
returns EOF.
2. The end of a file is simply the position in the file at which it
ends, depending on its current size.
3. An "end-of-file indicator" is an internal flag associated with a
stream. It's set when a function tries and fails to read past the
end of the file. It's cleared when the file is first opened, and
by fseek(). You can query it with feof(), but you seldom need to.
These are three entirely different things.
A "stream" is an entity in your executing program that's associated
with an external file. The file exists outside your program,
typically sitting on a disk somewhere (though there are a myriad of
other possibilities). fopen() associates a stream (in your program)
with the file (outside your program), and gives you a FILE* that you
can use to operate on the stream, and therefore on the file. The
"end-of-file indicator" is associated with the stream; it doesn't
exist in the external file. Similarly, your current position in the
file (as set by fseek(), or updated when you read data from the file)
is associated with the stream; it doesn't exist in the external file.
So, you call fseek(), which, if successful, clears the "end-of-file
indicator" for the stream (#3 above). (If you attempt to fseek() past
the end of the file (#2 above), it might succeed, or it might fail,
depending on the implementation. Don't try it unless you know what
you're doing.) fseek() has *no effect* on the external file; the end
of the file (#2 above) is not touched.
Now you call fread() on the file. If the fread() attempts to read
more data than there is between your current position and the end of
the file (#2), it will fail; it will indicate this by telling you that
it returned fewer items than you asked it to. This will also set
the end-of-file indicator (#3).
If you had called fgetc() rather than fread(), it would be the same
situation, except that fgetc() would indicate failure by returning the
value EOF (#1 above). If you're using fread() rather than fgetc(),
you probably won't be using the EOF macro at all.
Have you read section 12 of the comp.lang.c FAQ yet?
<http://www.c-faq.com/>
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"