fseek() newbie question

E

ericunfuk

Hi,

In my program, I'm trying to seek forwards and backwards in a file
using fseek(),and use fread() to read a chunck of the file and send
that chunk off. I needed to send the file in chunks and sometimes I
need to resend some chunks if they get lost on their way, so I use
fseek() to seek back to the chunk that was lost and resend. But I
can't detect the EOF indicator as fseek() cleared it?So is there a way
I could check EOF has been reached or will be reached? I tried feof()
but I found that it's the worng thing to try, as it only tells you
"after" your attempt to move beyond EOF which may already been removed
by feek() in my case as well.


Thanks for any help you could provide.

Eric
 
J

Jens Thoms Toerring

ericunfuk said:
In my program, I'm trying to seek forwards and backwards in a file
using fseek(),and use fread() to read a chunck of the file and send
that chunk off. I needed to send the file in chunks and sometimes I
need to resend some chunks if they get lost on their way, so I use
fseek() to seek back to the chunk that was lost and resend. But I
can't detect the EOF indicator as fseek() cleared it?So is there a way
I could check EOF has been reached or will be reached? I tried feof()
but I found that it's the worng thing to try, as it only tells you
"after" your attempt to move beyond EOF which may already been removed
by feek() in my case as well.

It's rather unclear what exactly your problem is. In which kind of
situation you can't detect EOF? And how would fseek() "remove" EOF?
Once you have reached the end of the file that condition (EOF) is
set and can be tested for by feof(). Of course, fseek() will clear
that condition if you move your current position in the file from
the very end to somewhere before that since then this condition is
not true anymore.

And you can never tell if the end of a file is going to be reached
before you actually do the read - there might be another process
that writes to the file and thus have extended it in between your
attempt of trying to figure out if the end will be reached and
your actually reading from it. For that reason trying to write a
function like feof() that would look into the future is doomed to
fail (even if you would tell it how many bytes you plan to read).

Regards, Jens
 
W

websnarf

In my program, I'm trying to seek forwards and backwards in a file
using fseek(),and use fread() to read a chunck of the file and send
that chunk off. I needed to send the file in chunks and sometimes I
need to resend some chunks if they get lost on their way, so I use
fseek() to seek back to the chunk that was lost and resend. But I
can't detect the EOF indicator as fseek() cleared it?So is there a way
I could check EOF has been reached or will be reached? I tried feof()
but I found that it's the worng thing to try, as it only tells you
"after" your attempt to move beyond EOF which may already been removed
by feek() in my case as well.

You can try to use fseek(fp, 0,SEEK_END) then ftell(fp) then just
figure out when you've reached the end by yourself as you go. (Or
just use fstat(), which is POSIX.) Of course, if the file is greater
than 2GB in length, then you will want to use the platform extensions
like _fseeki64 or _ftelli64 which may exist on your platform if it is,
say, Windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fseek 17
fseek() 6
fseek() 1
feof(), fseek(), fread() 20
Determining EOF using fseek()? 6
fseek() clears EOF? 6
Text mode fseek/ftell 10
Question about change of "fp" in function "fseek"and "ftell" 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top