How to clear input stream in ANSI C? 
Author Message
 How to clear input stream in ANSI C?

hi, Dear all
    I encountered this problem:
#include <stdio.h>

int main()
{
    char i,j;
    printf("Input i: ");
    scanf("%c",&i);
    printf("input j: ");
    scanf("%c",&j);

    printf("i: %c,  j: %c \n",i,j);

return 0;

Quote:
}

On running:
Input i: abcd       //The input stream contains: 'a','b','c','d','\n', so
the second scanf just finished automatically.
input j: i: a,  j: b

Is there any method to clear the input stream that the second scanf() can
take effect?

Thanks a lot!



Sat, 10 Sep 2005 12:58:48 GMT  
 How to clear input stream in ANSI C?

Quote:

> hi, Dear all
>     I encountered this problem:
> #include <stdio.h>

> int main()
> {
>     char i,j;
>     printf("Input i: ");
>     scanf("%c",&i);
>     printf("input j: ");
>     scanf("%c",&j);

>     printf("i: %c,  j: %c \n",i,j);

> return 0;
> }

> On running:
> Input i: abcd       //The input stream contains: 'a','b','c','d','\n', so
> the second scanf just finished automatically.
> input j: i: a,  j: b

> Is there any method to clear the input stream that the second scanf() can
> take effect?

> Thanks a lot!

I don't think I understand your question. Do you want to ignore the b, c,
d, etc?

Maybe you want fgets()? It reads a line at a time into a buffer. Or you
could use getchar() to read the first char, then use getchar() in a loop to
read until you get a '\n'.

Here is a working example. I believe it is logically equivalent to your
code except that it ignores all the characters after the first one on each
line.

I hope this helps.

regards,
Mac

#include <stdio.h>

int main(void)
{
  int c, i, j;

  printf("Input i: ");
  fflush(stdout);
  i = getchar();

  do {
    c = getchar();
  } while (c != EOF && c != '\n');

  printf("input j: ");
  fflush(stdout);
  j = getchar();

  do {
    c = getchar();
  } while (c != EOF && c != '\n');

  printf("i: %c,  j: %c \n",i,j);
  return 0;

- Show quoted text -

Quote:
}



Sat, 10 Sep 2005 15:12:20 GMT  
 How to clear input stream in ANSI C?
Not sure..but i do think..this wont fail...as an input stream flush...

char i,j;
printf("input i: ");
i = (char)0;
scanf("%c",&i);
printf("input j: ");
j = (char)0;
scanf("%c",&j);

Quote:

> hi, Dear all
>     I encountered this problem:
> #include <stdio.h>

> int main()
> {
>     char i,j;
>     printf("input i: ");
>     scanf("%c",&i);
>     printf("input j: ");
>     scanf("%c",&j);

>     printf("i: %c,  j: %c \n",i,j);

> return 0;
> }

> On running:
> Input i: abcd       //The input stream contains: 'a','b','c','d','\n', so
> the second scanf just finished automatically.
> input j: i: a,  j: b

> Is there any method to clear the input stream that the second scanf() can
> take effect?

> Thanks a lot!



Sat, 10 Sep 2005 15:54:19 GMT  
 How to clear input stream in ANSI C?

<snip>

Quote:
> Is there any method to clear the input stream that the second scanf() can
> take effect?

Just keep reading bytes from the input stream until you encounter either
end-of-file or a byte that you are satisfied means "end of the input you
don't care about".

while((ch = getchar()) != EOF && ch != '\n')
{
  continue;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Sat, 10 Sep 2005 16:07:37 GMT  
 How to clear input stream in ANSI C?

Quote:
> Not sure..but i do think..this wont fail...as an input stream flush...
> char i,j;
> printf("input i: ");
> i = (char)0;
> scanf("%c",&i);
> printf("input j: ");
> j = (char)0;
> scanf("%c",&j);

The assignments to i and j have no effect whatsoever on the contents
of the input stream. Please see rjh's reply for a more correct version.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
   - Mickey Mouse



Sat, 10 Sep 2005 16:29:21 GMT  
 How to clear input stream in ANSI C?
Thanks , now I have solved this problem with you all 's help.
And in VC,  _flushall() can clear the input stream, so can fflush(stdin)

but in gcc on linux, it doesn't have _flushall() and fflush just clear the
output stream.
So we have to use the code below  :)

Quote:

> while((ch = getchar()) != EOF && ch != '\n')
> {
>   continue;
> }

> --

> "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sat, 10 Sep 2005 18:44:08 GMT  
 How to clear input stream in ANSI C?

Quote:

> Thanks , now I have solved this problem with you all 's help.
> And in VC,  _flushall() can clear the input stream, so can
> fflush(stdin)

You might also want to have a look at the c.l.c FAQ:
http://www.eskimo.com/~scs/C-faq/top.html

In particular, see Section 12 (Stdio):
http://www.eskimo.com/~scs/C-faq/s12.html

And please don't top-post!  Thank you.



Sat, 10 Sep 2005 19:51:48 GMT  
 How to clear input stream in ANSI C?
On Tue, 25 Mar 2003 18:44:08 +0800, in comp.lang.c , "Zhiqiang Ye"

Quote:

>Thanks , now I have solved this problem with you all 's help.
>And in VC,  _flushall() can clear the input stream, so can fflush(stdin)

Only if its docs say so. fflush() is not defined for input streams,
and is as likely to dump core as work. _fflushall() is not even part
of C.

If you think about it, clearing input streams is a bit problematical.
It need not have an end, for instance.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sun, 11 Sep 2005 06:56:40 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. clearing input streams

2. string stream clear

3. clear characters in a stream-buffer

4. Newbie: separate big .cs file into small .cs files

5. clear the input buffer?

6. HELP: clearing input buffer in c

7. Unable to sync input and output streams

8. c file (stream) input buffer problem help PLEASE!

9. flushing the standard input stream (newb question)

10. ANN: Numeric input from file streams

11. state of input stream after bad scanf ?5

12. Problem with input stream redirection in lex

 

 
Powered by phpBB® Forum Software