[NTLUG:Discuss] fflush dosn't work

Daniel Hauck xdesign at hotmail.com
Sun Jul 14 23:03:22 CDT 2002


Okay okay... seriously now. :)

scanf() is not used for character streams from terminal devices.  For that,
you will want to use fgetc() or some other variant of getchar().  For more
information, check "man fgetc"

Also, to prove I know what I'm talking about, you will find that if you
press "y" and then enter, that it will exit as you expected it to.  scanf()
is a function designed for processing lines and is best used on formatted
data such as that which comes from a predictable IO stream such as a piped
output from a program delivering predictable output.  (Generally, using
fscanf() is preferred over scanf() as a matter of program clarity.)

I hope you liked both my joke and serious answers. :)

----- Original Message -----
From: "Geremy L. Hamlett" <ghamlett at swbell.net>
To: "North Texas Linux Users Group" <discuss at ntlug.org>
Sent: Sunday, July 14, 2002 22:34
Subject: [NTLUG:Discuss] fflush dosn't work


> I am having a problem with some .c code. Specificlly w/ the fflush syntax.
>
> Here is some sample code:
>
> #include <stdio.h>
>
> int main(void){
>
> char ch = 'N';
>
> while (ch != 'y'){
> printf("Enter a letter:");
> fflush(stdin);
> scanf("%c", &ch);
> }
>
> return (0);
> }
>
>
> Basicly what happens is it enters the loop, and prompts to enter a letter.
> Then after the input the loop checks the true/false condition of the loop.
>
> When you type a letter and then enter, it then checks.  The fflush command
> is suposed to flush the input and remove the \r token, But it dosn't.
>
> after compiling w/ gcc and run it. The fflush dosn't seam to remove any
> thing.
>
> What Do I need to do to utilize fflush?
>
> Thanks for your help,
> --
> Geremy L. Hamlett
>
> http://omega.uta.edu/~glh6688
> "All Your Base are Belong to Us"
>
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>




More information about the Discuss mailing list