--- In [email protected], shadyabhi <abhijeet.1...@...> wrote:
>
> fflush() is only used for flushing OUTPUT buffer.
Portably yes.
> But how do I flush input buffer?
Why do you want to do that?
Define what you mean by flushing an input buffer. The reason
it's not defined on input streams in standard C is that
there are multiple possible things it could be.
> One way is using:-
>
> while ((ch = getchar()) != '\n' && ch != EOF);
>
> IS there any OTHER WAY?
If it does what you want, what's wrong with it?
You can do stuff like scanf("%[^\n]"); but that this isn't
guaranteed to flush a line either.
I suspect your reason for asking is that you actually
want an INKEY or getch() style behaviour. If that's
so, then you should look at ncurses.
But if you really just want to throw away trailing
crud from a line of input, then one way is read a line
at a time, e.g. with fputs(), and simply ignore what you
don't need.
> I also tried using library function:- setbuf(stdin,NULL);
> but no success..
That simply removes stdin's cache within the standard libary.
It won't change the behaviour of getchar().
Can you give us a context (program) where you want this
to happen.
--
Peter