Re: Standard input stream behaviour on Linux
Tomás Ó hÉilidhe <toe@lavabit.co mwrites:
No, there isn't.
My guess is you're imagining that the user types a number and then a
newline, and that stdin has a buffer that contains these characters. It
might, but it might also contain fewer (if the terminal is in a "raw"
mode, for example), or more (if the user types ahead).
What exactly do you mean by "lingering" and "flush"? I think if you try
to pin down these definitions, you'll find that either it isn't what
you want, it's impossible, or it's easily accomplished another way.
In this case, I think you really want to discard everything until the
end of the line, so you could just do that.
while (getchar() != '\n') ;
This isn't much of an improvement over just calling fgets() in the first
place, however, which IMHO would be less confusing.
Tomás Ó hÉilidhe <toe@lavabit.co mwrites:
Is there any way of finding out how many character there are
"lingering" in stdin?
>
If so, I could flush it as follows:
>
void FlushStdin(void )
{
unsigned i = AmountLingering ();
>
while (i--)
getchar();
}
>
I could call FlushStdin before I use fgets.
"lingering" in stdin?
>
If so, I could flush it as follows:
>
void FlushStdin(void )
{
unsigned i = AmountLingering ();
>
while (i--)
getchar();
}
>
I could call FlushStdin before I use fgets.
My guess is you're imagining that the user types a number and then a
newline, and that stdin has a buffer that contains these characters. It
might, but it might also contain fewer (if the terminal is in a "raw"
mode, for example), or more (if the user types ahead).
What exactly do you mean by "lingering" and "flush"? I think if you try
to pin down these definitions, you'll find that either it isn't what
you want, it's impossible, or it's easily accomplished another way.
In this case, I think you really want to discard everything until the
end of the line, so you could just do that.
while (getchar() != '\n') ;
This isn't much of an improvement over just calling fgets() in the first
place, however, which IMHO would be less confusing.
Comment