fflush not flusing stdin!

J

jaswinder

Greetings:

While working on a computer science assignment I have had the need to
flush the contents of stdin. But the statement

fflush(stdin);

doesn't seem to be working. On the other hand:

char s[80];

gets(s);

does seem to be working provided that whatever is in the stdin is not
longer than 80 characters.

Could anyone kindly point out why fflush failed in this situation.
 
I

Ian Collins

Greetings:

While working on a computer science assignment I have had the need to
flush the contents of stdin. But the statement

fflush(stdin);

doesn't seem to be working. On the other hand:

char s[80];

gets(s);

does seem to be working provided that whatever is in the stdin is not
longer than 80 characters.

Could anyone kindly point out why fflush failed in this situation.

C99 7.19.5.2 The fflush function

2 If stream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.

In other words, you can't use fflush on stdin.
 
S

Seebs

In other words, you can't use fflush on stdin.

If this seems weird, here's a way to think about why that limitation makes
sense:

How much data have you written to stdin recently?

If you haven't written anything to stdin, what makes you think there is
any data that you've written which remains unflushed?

To "flush" a buffer is not to "discard", it's to "ensure completion of
writing". If you want to *discard* data, you need something else. (The
Berkeley libc has fpurge() for exactly that, other systems may overload
fflush() to do it, despite the misleading semantics, or not provide a means,
or...)

-s
 
N

Nick Keighley

While working on a computer science assignment I have had the need to
flush the contents of stdin.

no you didn't!

But the statement

fflush(stdin);

doesn't seem to be working.

[line moved]
Could anyone kindly point out why fflush failed in this situation.

as others have pointed out fflush() only works for (in a sense only
has a meaningful defintion for) output streams. I'm not sure exactly
what you expect but one possibility is you want to skip to the end of
line.

int skip_line (FILE *instream)
{
int ch;
while ((ch = gets(instream)) != '\n' && ch != EOF)
;
return ch;
}

functions like this are tricker to write than you might expect, so
expect a few brickbats to come my way!
On the other hand:

char s[80];

gets(s);

oooH! NEVER DO THAT

gets() is borken. It has no way to indicate how big its buffer is.
(fgets() can be used instead, though it doesn't do quite the same
thing as gets(), read the documentation).
[gets()] does seem to be working provided that whatever is in
the stdin is not longer than 80 characters.

and if it is... where do those extra characters go? On most systems
all over your other data. This is the way in for an amazing number of
virii and other malware.

http://en.wikipedia.org/wiki/Conficker
(ok, I don't know it was gets(), but it *was* a buffer overflow!)

You may want to read this
http://c-faq.com/

Particularly:-
12.26a How can I flush pending input so that a user's typeahead isn't
read at the next prompt? Will fflush(stdin) work?

12.26b If fflush won't work, what can I use to flush input?

12.23 Why does everyone say not to use gets()?

In fact read all of section 12. Then read all of the FAQ. Some you
might not understand or haven't studied yet and some may seem over
picky. Re-read when you learn a little more C and you'll appreciate it
more.



--

A ruby trembled. Two tourmaline nets failed to rectify the laser beam.
A diamond noted the error. Both the error and the correction went into
the general computer.
Corwainer Smith "The Dead Lady of Clown Town"
 
J

Jens Thoms Toerring

Nick Keighley said:
int skip_line (FILE *instream)
{
int ch;
while ((ch = gets(instream)) != '\n' && ch != EOF)
^^^^

I am 100% sure you meant getc() instead of gets() here;-)

Regards, Jens
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top