almost understood

E

ericunfuk

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?
 
B

Bill Pursell

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
 
S

santosh

ericunfuk said:
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.
 
E

Eric Sosman

santosh said:
ericunfuk said:
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.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top