Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fflush not flusing stdin!

Reply
Thread Tools

fflush not flusing stdin!

 
 
jaswinder
Guest
Posts: n/a
 
      08-18-2010
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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-18-2010
On 08/19/10 09:56 AM, jaswinder wrote:
> 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.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      08-18-2010
On 2010-08-18, Ian Collins <(E-Mail Removed)> wrote:
> On 08/19/10 09:56 AM, jaswinder wrote:
>> 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.


> 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
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-19-2010
On 18 Aug, 22:56, jaswinder <(E-Mail Removed)> wrote:

> 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"
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-19-2010
Nick Keighley <(E-Mail Removed)> wrote:
> 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
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fflush(stdout); Rajesh S R C Programming 6 04-08-2007 12:40 PM
fflush() Jack C Programming 5 07-17-2006 09:23 PM
when fflush call is needed? baumann@pan C Programming 5 06-07-2005 02:01 AM
In fflush(stdin), what happens to flushed data? hugo27 C Programming 5 10-10-2004 04:59 PM
<cstdio> and fflush(stdout) zerotypeepytorez@yahoooohay.commoc C++ 3 08-10-2004 10:57 PM



Advertisments