Connecting Tech Pros Worldwide Help | Site Map

almost understood

ericunfuk
Guest
 
Posts: n/a
#1: Mar 24 '07
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?

Bill Pursell
Guest
 
Posts: n/a
#2: Mar 24 '07

re: almost understood


On Mar 24, 10:58 am, "ericunfuk" <xuwenduan2...@gmail.comwrote:
Quote:
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

santosh
Guest
 
Posts: n/a
#3: Mar 24 '07

re: almost understood


ericunfuk wrote:
Quote:
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.

Eric Sosman
Guest
 
Posts: n/a
#4: Mar 24 '07

re: almost understood


santosh wrote:
Quote:
ericunfuk wrote:
Quote:
>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
esosman@acm-dot-org.invalid

Closed Thread