473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(forw ards 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

Mar 24 '07 #1
20 7490
"ericunfuk" <xu***********@ gmail.comwrites :
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(forw ards 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
Mar 24 '07 #2
On 24 Mar, 01:40, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
"ericunfuk" <xuwenduan2...@ gmail.comwrites :
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(forw ards 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.

Mar 24 '07 #3
"ericunfuk" <xu***********@ gmail.comwrites :
On 24 Mar, 01:40, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
>"ericunfuk" <xuwenduan2...@ gmail.comwrites :
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(forw ards 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
bl*@cs.stanford .edu
http://benpfaff.org
Mar 24 '07 #4
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)
Mar 24 '07 #5
Sjouke Burry <bu************ *@ppllaanneett. nnlllwrites:
>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_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"
Mar 24 '07 #6
"ericunfuk" <xu***********@ gmail.comwrites :
On 24 Mar, 01:40, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
>"ericunfuk" <xuwenduan2...@ gmail.comwrites :
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(forw ards 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_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"
Mar 24 '07 #7
After fseek(), call fread() to read data. If it returns a short
--
Ben Pfaff
b...@cs.stanfor d.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?


Mar 24 '07 #8
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.

Mar 24 '07 #9
"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"
Mar 24 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2432
by: deko | last post by:
I'm trying to control the size of a log file. When it gets more than 5k oversize, I attempt to trim it thusly: $fcap = intval(($dval * 0.0108))*1024; // what it should be $fsize = filesize($file); // what it is $oversize = $fsize - $fcap; if ( $oversize > 5120 ) // 5k oversize { $fp = fopen($file,"r+");
10
5937
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows, where text files use CF+LF (0x0d, 0x0a) to mark end-of-line. The file in question, however, was in Unix format, with only LF (0x0a) at the end of...
14
3687
by: Maria Mela | last post by:
Hello everyone... I´ve a problem with my code, when i put this lines: recsize = sizeof(p1); fseek(fp,-recsize,SEEK_CUR); fwrite(&p1,sizeof(p1),1,fp); getch(); The file was saved with correct values and with some windows informations too!?
6
6124
by: ericunfuk | last post by:
A basic question: When the documentation says "fseek() clears EOF indecator, does it mean when you seek over EOF, EOF is no longer at the original position and hence removed?Say, after I seek over the original EOF, when I fread() from a previous position that I know is before the EOF then fread will not be able to tell if it has encountered...
6
3729
by: ericunfuk | last post by:
I have posted a few other posts before this one today, I still can't solve my problem, I think I need to elaborate my problem now. I'm trying to send a file using UDP, below is a segment of my sender app, the sender and the receiver are both on the same machine and I have an Internet emulation gateway running on the same machine as well. ...
7
5912
by: ahmetbm | last post by:
Hi; I have a c program that uses mpi library to run on parallel arhitecture. First i coded serial program and it worked perfectly. But the parallel one did not run. I found the error but could not solve it. The error is: the "fseek" and "fread" functions does not return the correct values. fseek(wav_fp,40L,SEEK_SET);fread(&str,4,1,wav_fp); ...
13
4475
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before I read the bytes. Determining the number of bytes available in a file is done in 5 steps: 1. Use tell(aFile) to get the current position.
24
5974
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h> #include<string.h> void main(){ FILE *fp,*ftemp;
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.