473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

fseek() clears EOF?

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 the original EOF?

Thank YOU!

Mar 23 '07 #1
6 6103
ericunfuk wrote:
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 the original EOF?
You need to separate the notions of "file" and "stream."

The "file" is the ultimate source or sink of data: It
could be a keyboard or a network interface or a sound board.
For ease of explanation, let's assume for now that it's a
fixed amount of data sitting on a disk somewhere.

The "stream" is the connection between the program and
a "file." It is the conduit through which data flows to and
from the file. For files where the notion makes sense, the
stream also keeps track of the "current position" within the
file, that is, the place in the file to or from which the
next character will be transferred.

Finally, the stream also maintains a few indicators to
help the program find its way. One of these is the EOF
indicator, which gets set whenever a read bumps up against
the end of the file.

With this in mind, you'll see that clearing the EOF
indicator does nothing at all to the file! It clears the
stream's "I've hit the end of the file" indicator, which
makes sense because when you fseek() you're telling the
stream to start over at another position in the file; the
information that "I'm at the end" is no longer accurate.

Here's a thought experiment -- or even an actual experiment;
go ahead and try it. Open two different input streams on the
very same file, simultaneously. Read from one of them, and keep
reading until you reach the end of the file. If you then read
from the second stream, what do you expect should happen?

Second thought experiment: Suppose the disk that holds the
file is a read-only device like a CD-ROM. In what ways can
fseek() change the file?

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 23 '07 #2
>It clears the
stream's "I've hit the end of the file" indicator, which
Here "clears" doesn't mean EOF is removed, right?i.e. it doesn't mean
the stream could go beyond the actual end of the file. e.g. if the
stream representing the original file is from memory location 0 to 10,
when I fseek() from position 9, 3 locations, you will still get the
EOF set for the stream right?9+3 is beyond the original stream
representing the original file.

Mar 23 '07 #3
wu*******@googlemail.com wrote:
>It clears the
stream's "I've hit the end of the file" indicator, which

Here "clears" doesn't mean EOF is removed, right?
The end-of-file indicator is part of the stream, not part
of the file. Again, I suggest you consider what changes are
made to a read-only file when you fseek() a stream that's
reading from it.
i.e. it doesn't mean
the stream could go beyond the actual end of the file.
Different file systems handle this differently, so C doesn't
try to tell them all how to behave on an off-the-end fseek().
On some systems the fseek() will fail. On others, the fseek()
may succeed but a subsequent attempt to read will fail. On
yet others, it is possible to fseek() past the end of the file
and start writing, producing a "sparse" file.

But none of these really has anything to do with "clearing
the end-of-file indicator." That indicator reports the state
of the stream, not of the file it's attached to.
e.g. if the
stream representing the original file is from memory location 0 to 10,
when I fseek() from position 9, 3 locations, you will still get the
EOF set for the stream right?9+3 is beyond the original stream
representing the original file.
Here you've lost me: I don't understand how "memory locations"
enter into the picture.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 23 '07 #4
In article <11*********************@n59g2000hsh.googlegroups. com>,
<wu*******@googlemail.comwrote:
>>It clears the
stream's "I've hit the end of the file" indicator, which
>Here "clears" doesn't mean EOF is removed, right?i.e. it doesn't mean
the stream could go beyond the actual end of the file. e.g. if the
stream representing the original file is from memory location 0 to 10,
when I fseek() from position 9, 3 locations, you will still get the
EOF set for the stream right?9+3 is beyond the original stream
representing the original file.
Your wording is a bit ambiguous.

What you need to know is:
- EOF is a value (always negative) that will be returned by some read
operations to signal that the end of stream has been reached (or
exceeded).

- There is an internal end-of-file indicator for each stream. That
indicator is almost always stored in a completely different way than by
setting something internal to the particular value EOF. So you have to
distinguish the -value- EOF that will be returned by some operations to
indicate that the end-of-file indicator is set.

- If you fseek() past the EOF, then doing so is a valid operation, and
clears the internal end-of-file indicator associated with the stream.

- In C, if the current position is at or beyond the end of the stream,
and you attempt to read more data, an appropriate end-of-file indicator
will be returned. The exact indicator depends on the exact variety of
read call but is often by returning the value EOF

- In C, the end-of-file indicator being set, as detectable by feof(),
does not mean that the system somehow "knows" that the next read
attempt would fail -- it is, for example, not set when you read the
last character of a stream. Instead, the end-of-file indicator flag, as
detectable by feof(), is only set when the program attempts a read and
that read does not succeed because the position is at or beyond the end
of the stream.

- There is good reason for the implementation NOT to set the
end-of-file indicator upon reading the last existing character: namely,
that there might be more data for the stream that will arrive later,
such as if the stream is associated with an input device (e.g., user &
keyboard) or the stream is associated with a network socket.

- Thus, the internal end-of-file indicator does not indicate that
the next I/O will be at (or beyond) the end of stream: it indicates
instead that the -previous- I/O was at (or beyond) the end of stream.
So the internal end-of-file indicator can be cleared in various ways
without affecting what would happen if another read were to be
attempted.
Additionally:
- The C89 standard does not define exactly what happens if you write
new data (on a writable stream) when you are positioned past the end of
the stream. There is another widely used standard for operating system
interfaces, POSIX.1, which does specify the behaviour in such a
situation, at least on systems that conform to that standard. (The
POSIX.1 standard says that in such a case that the write is valid, and
that all data between the previous end of stream and the new position
will read back as binary zeroes unless other data is written into the
gap.)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Mar 23 '07 #5
On 23 Mar 2007 13:06:04 -0700, "ericunfuk" <xu***********@gmail.com>
wrote:
>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 the original EOF?
The EOF indicator is set only when you try to read past the last
character in the file, not when you read the last character. If you
fseek(), you are repositioning the file such that you not yet tried
to read past the end. Even when you fseek() to the end of the file,
you are positioning at the end, as if you had just read the last
character but no more. Since you have not tried to read past the last
character, the indicator is reset.
Remove del for email
Mar 23 '07 #6
Barry Schwarz <sc******@doezl.netwrites:
On 23 Mar 2007 13:06:04 -0700, "ericunfuk" <xu***********@gmail.com>
wrote:
>>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 the original EOF?

The EOF indicator is set only when you try to read past the last
character in the file, not when you read the last character. If you
fseek(), you are repositioning the file such that you not yet tried
to read past the end. Even when you fseek() to the end of the file,
you are positioning at the end, as if you had just read the last
character but no more. Since you have not tried to read past the last
character, the indicator is reset.
I think it's important to keep the terminology straight. There is no
"EOF indicator"; there is an "end-of-file indicator".

The end-of-file indicator is an internal flag, associated with a
stream, that indicates that records whether the end of the file has
been reached (C99 7.19.1p2). The feof() function can be used query
the end-of-file indicator, but that's less useful than you might
think.

EOF is a macro, defined in <stdio.h>, "which expands to an integer
constant expression, with type int and a negative value, that is
returned by several functions to indicate end-of-file, that is, no
more input from a stream" (C99 7.19.1p3).

If, for example, you're reading a file using the fread() function,
then you can detect an end-of-file condition by checking the value
returned by fread(); EOF is not involved.

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

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

Similar topics

62
by: Christopher Benson-Manica | last post by:
On thinking about the "replace a word in a file" thread, I wondered how easy it would be to accomplish the same thing with only one file pointer. This led me to some questions... "For a text...
15
by: TJ Walls | last post by:
Hello All, I am baffled ... I am trying to improve the speed of a program that I have written that performs random access within a file. It relies heavily on fseek and is very slow. To test, I...
2
by: cedarson | last post by:
I am writing a program and have been instructeed to use the 'fseek', 'ftell', and 'stat' functions, however, after looking in the online manual for each of these, I am still unsure on how to use...
10
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,...
3
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); ...
23
by: Dmitry Belous | last post by:
Why when I uncomment fseek line buf become empty? #define _GNU_SOURCE #include <stdio.h> int main() { char *buf = 0; int size = 0; FILE* f = open_memstream(&buf, &size); fwrite("test", 1, 4,...
14
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...
1
by: ericunfuk | last post by:
Does fseek() clears EOF if I only fseek() within the file stream. i.e. I don't try to seek beyond EOF?
20
by: ericunfuk | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.