Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

feof(), fseek(), fread()


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.

Or better still, could anyone let me know some good ways to achieve
what I need to do as above?Can I get hold of the file and being able
to read in without using fread()? Using memory addresses?

Thanks

"ericunfuk" <xuwenduan2@gmail.com> 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
On 24 Mar, 01:40, Ben Pfaff <b@cs.stanford.edu> wrote:

> "ericunfuk" <xuwenduan2@gmail.com> 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.

"ericunfuk" <xuwenduan2@gmail.com> writes:
> On 24 Mar, 01:40, Ben Pfaff <b@cs.stanford.edu> wrote:
>> "ericunfuk" <xuwenduan2@gmail.com> 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.

> 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.

After fseek(), call fread() to read data.  If it returns a short
read, you have either encountered an error or end of file.  Use
ferror() and feof() to distinguish an error from end of file.

This is what I said last time.  How is it inadequate?
--
Ben Pfaff
b@cs.stanford.edu
http://benpfaff.org

> 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.

If you read the docs for fseek, you find you
can seek to the end of the file.
ftell(fp) then tells you the total size, after
which you can get at any part of the file,
by seeking to the required location, and without ever
needing to use EOF.(unless you go past wat ftell told you)

Sjouke Burry <burrynulnulf@ppllaanneett.nnlll> writes:
>> 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.

> If you read the docs for fseek, you find you
> can seek to the end of the file.
> ftell(fp) then tells you the total size, after
> which you can get at any part of the file,
> by seeking to the required location, and without ever
> needing to use EOF.(unless you go past wat ftell told you)

fseek() to the end of a binary file is not reliable.

--
Keith Thompson (The_Other_Keith) k@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"

"ericunfuk" <xuwenduan2@gmail.com> writes:
> On 24 Mar, 01:40, Ben Pfaff <b@cs.stanford.edu> wrote:
>> "ericunfuk" <xuwenduan2@gmail.com> 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.

> 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.

Please don't quote signatures.

You keep talking about clearing EOF.  What you really mean is clearing
the end-of-file indicator for a stream.  EOF is something different
(even though it's an abbreviation of End Of File).

The end-of-file indicator indicates that the last attempt to read from
the file failed because you attempted to read past the end of the
file.  The way to detect this is to check the result of whatever
function you're using to read from the file.  If you use fread()
(which is perfectly reasonable), check the value it returns; if it
tells you that it read fewer items than you asked it to, then you've
probably reached the end of the file.

It's also possible that there's been an error of some sort; the
ferror() function can tell you this.

--
Keith Thompson (The_Other_Keith) k@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"

> After fseek(), call fread() to read data.  If it returns a short
> --
> Ben Pfaff
> b@cs.stanford.eduhttp://benpfaff.org

How about fseek() already seeked over EOF before I invoke fread(), and
it could cleared EOF, so feof() or ferror() won't work anymore?
I'm really confused!!!
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?

Anyone enlighten me.

"ericunfuk" <xuwenduan2@gmail.com> writes:
> 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_Keith) k@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"

ericunfuk <xuwenduan2@gmail.com> wrote:
>in my understanding, fread() would not encounter the original
>end-of-file it will read beyond x and no error(even EOF) will happen?

The question is, are you calling fread() anywhere past the end of the
file?

If the answer is yes, then feof() will return non-zero right after that.

Think of it this way: end-of-file is not something you cross like a
border.  It's a place that you either are in, or are not in.

You're trying to fool the end-of-file flag by calling fseek() to clear
the flag before calling fread() past the end of the file.  But it won't
work, because fread() will see that it is once again past the end of the
file, and it will just set the end-of-file flag again!

-Beej

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc