By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

almost understood

P: n/a
Suppose fread() has failed because it encountered end of file and it
returned less items than it should have, now after this failure where
will the file position indicator be set to?Just at the end of the file?

Mar 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 24, 10:58 am, "ericunfuk" <xuwenduan2...@gmail.comwrote:
Suppose fread() has failed because it encountered end of file and it
returned less items than it should have, now after this failure where
will the file position indicator be set to?Just at the end of the file?

If fread hits the end of file and thus returns fewer values
than you asked for, I don't think it's appropriate to say
that it failed. It in fact gave you exactly as many items
as it should have. That amount may have been less than
you asked for, but it was not less than the amount that
it "should have" returned.

As to the position of the file pointer, it is probably left
at the end of the file, but that may be implementation
dependent. Here's one way to test it:

[tmp]$ cat c.c

#include <stdio.h>

int
main( void )
{
char b;
while (fread( &b, 1, 1, stdin ))
;
printf("%ld\n", ftell( stdin ));

return 0;
}
[tmp]$ ./a.out < c.c
185
[tmp]$ wc -c c.c
185 c.c

--
Bill Pursell

Mar 24 '07 #2

P: n/a
ericunfuk wrote:
Suppose fread() has failed because it encountered end of file and it
returned less items than it should have, now after this failure where
will the file position indicator be set to?Just at the end of the file?
The end-of-file indicator can be set if either all the characters have
been read or if there was an I/O error. To find out which it was, you
can use feof/ferror immediately after the call to fread. If the
failure of fread was due to an error, (as opposed to a true end-of-
file condition), then the file position indicator is indeterminate.

Mar 24 '07 #3

P: n/a
santosh wrote:
ericunfuk wrote:
>Suppose fread() has failed because it encountered end of file and it
returned less items than it should have, now after this failure where
will the file position indicator be set to?Just at the end of the file?

The end-of-file indicator can be set if either all the characters have
been read or if there was an I/O error. To find out which it was, you
can use feof/ferror immediately after the call to fread. [...]
Not quite: The end-of-file indicator gets set when an
operation encounters the end of the file, and the error
indicator gets set when an operation encounters an error.
An I/O error does not set the end-of-file indicator (not
on its own account, anyhow; it's possible that some system
might encounter I/O errors during some kind of automatic
cleanup after end-of-file, so a single operation could set
both indicators -- but an I/O error, all by itself, doesn't
have anything to do with the end-of-file indicator).

Perhaps you're confusing the two indicators with the
single "it didn't work" value returned by an I/O function.
That value (EOF for some functions, NULL for others) declares
that something went wrong but doesn't say what: it could have
been end-of-file or it could have been an error (or perhaps
both). That's when you use feof() and ferror() to disambiguate.

--
Eric Sosman
es*****@acm-dot-org.invalid

Mar 24 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.