question on clearerr() function

S

subramanian100in

Suppose I open a text file in 'read' mode and assume fopen is
successful. Suppose this input file contains only few characters and
no new line(I am working on LINUX based machine. In this environment I
can create the above input file as follows:
cat > input
123 followed by ctrl-d twice. Then this 'input' file will contain only
123 and no end of line)

If I use fgets on this 'input' file, it will fetch '123' and EOF
indicator will be set. Now if I say clearrerr(fp) where fp was
returned by fopen, then the end of file indicator on fp should be
cleared by clearerr call. Does 'clearing the end of file indicator'
mean that we can start reading input from the same stream from the
beginning of the file ? If not, what does 'clearing the end of file
indicator' mean ?

Kindly clarify.

Thanks
V.Subramanian
 
K

Keith Thompson

Suppose I open a text file in 'read' mode and assume fopen is
successful. Suppose this input file contains only few characters and
no new line(I am working on LINUX based machine. In this environment I
can create the above input file as follows:
cat > input
123 followed by ctrl-d twice. Then this 'input' file will contain only
123 and no end of line)

If I use fgets on this 'input' file, it will fetch '123' and EOF
indicator will be set. Now if I say clearrerr(fp) where fp was
returned by fopen, then the end of file indicator on fp should be
cleared by clearerr call. Does 'clearing the end of file indicator'
mean that we can start reading input from the same stream from the
beginning of the file ? If not, what does 'clearing the end of file
indicator' mean ?

The end-of-file indicator tells you that the *last* read attempt hit
the end of the file. Clearing the indicator just clears an internal
bit (possibly a member of the FILE structure, but that's an irrelevant
detail). It doesn't change your current position. Trying to read
from the file again will immediately set the end-of-file indicator
again.

Except that if more data has been written to the file in the meantime
(by something other than your program), you should now be able to read
it.
 
C

Chris Torek

The end-of-file indicator [in a stdio stream] tells you that the
*last* read attempt hit the end of the file. Clearing the indicator
just clears an internal bit (possibly a member of the FILE structure,
but that's an irrelevant detail). It doesn't change your current
position.
Right.

Trying to read from the file again will immediately set the
end-of-file indicator again.

Provided, of course, that the file position is still "end of file":
Except that if more data has been written to the file in the meantime
(by something other than your program), you should now be able to read
it.

Right.

There is one more important point, which is the reason I write this
article at all. In C89 / C90, a stdio implementation is permitted
to retry the read-on-underlying-file with or without the clearerr(),
but in C99, the clearerr() is required.

Consider the following code fragment:

FILE *fp;
int c;

... some code to make "fp" useful ...

while ((c = getc(fp)) != EOF)
continue; /* discard up to current EOF */

while (!ferror(fp)) {
#ifdef SOMETHING
if (c == EOF)
clearerr(fp);
#endif
c = getc(fp);
if (c != EOF)
putchar(c);
#ifdef SOMETHING_ELSE
else
... do something to avoid chewing up too much CPU time ...
#endif
}

This code is designed to print *only* "new" text added to a file
(a la "tail -f", on systems that have "tail", except that "tail -f"
includes some code to prevent chewing up too much CPU time).

The macro SOMETHING *must* be defined if the implementation is
a C99 implementation, otherwise the final loop will spin forever,
because each getc() will not actually bother trying to do a POSIX
read() call on the underlying file (fileno(fp)).

The macro SOMETHING *may* be defined on earlier (C89, C90, C95)
implementations, and on some implementations, *must* be defined.
That is, it never harms anything to enable the call to clearerrr(),
and on some (but not all) implementations, it is required. So you
should do it, if you are going to do this slightly odd thing (the
equivalent of "tail -f", that is) in the first place.
 
V

vippstar

There is one more important point, which is the reason I write this
article at all. In C89 / C90, a stdio implementation is permitted
to retry the read-on-underlying-file with or without the clearerr(),
but in C99, the clearerr() is required.

<snip explanation>

Thanks, I was unaware of this.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top