Re: feof(), fseek(), fread()



ericun*** wrote:
On 24 Mar, 01:40, Ben Pfaff <b...@xxxxxxxxxxxxxxx> wrote:
"ericun***" <xuwenduan2...@xxxxxxxxx> writes:
If fseek() always clears EOF, is there a way for me to fread() from an
offset of a file and still be able to detect EOF?i.e. withouting using
fseek(). I also need to seek to an offset in the file
frequently(forwards and backwards) and do fread() from that offset.
Please explain how the several previous answers to your several
previous similar questions are inadequate.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman

I still don't know how can I detect EOF while I'm always use fseek()
(It always clears EOF), and I must be able to seek forwards and
backwards in the file, but I still don't know up to now how can I do
this without fseek(), that's why I posted this question.

Some part of your C education has been missed. First, EOF is a macro defined in stdio.h probably as..

#define EOF (-1)

...It has a negative value of type int. You keep talking about fseek() clearing EOF. Wrong. Nothing about EOF can be cleared.

Within the file system there is there is presumably an indicator associated with the feof() function to tell us whether the file pointer (no, not FILE pointer) is at the end of the file. For sake of this discussion, let's call the flag eof (not EOF, which is a macro). This flag ivolves itself with attempts to read past the end of file with fgetc(), fgets(), etc. fseek() has nothing to do with reading from a file. There is nothing that fseek() can do that ought to set the eof flag.

If you would navigate within the file you can know its bounds (as offsets into the file) with two commands..

long eoff;
fseek(fp, 0, SEEK_END);
eoff = ftell(fp);

File offsets are now 0 to (not including) eoff, which is one past your playground. You can't seek past eoff. Attempts to do it will fail.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.