Heissenberg's getchar() 
Author Message
 Heissenberg's getchar()

I don't understand how to use int getchar() properly.
How do I test if anything is in the stdin stream?  If I
do a call to getchar() and there's nothing in stdin, then
execution stops and the program waits for keyboard input.
On the other hand if I assume nothing is in stdin, then
my assigning the return value of getchar to some variable
may pick up trash that was left over from some previous
function call.

So,
1) How can I test if anything is in the stdin stream?
--- and/or ---
2) How do I flush stdin without testing it?
   fflush(stdin) is undefined, right?

Thanks,
Bob

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()

Quote:

>I don't understand how to use int getchar() properly.
>How do I test if anything is in the stdin stream?  If I
>do a call to getchar() and there's nothing in stdin, then
>execution stops and the program waits for keyboard input.

I don't think you can get around this in a platform
independent way. You'll need to get under the hood
a bit more.


Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()

Quote:

> 1) How can I test if anything is in the stdin stream?

You can't.  C doesn't support that operation.  If you really need
to do so, you'll have to use a system extension.

Quote:
> 2) How do I flush stdin without testing it?

Again, you can't without system-specific extensions.

Quote:
>    fflush(stdin) is undefined, right?

Yes.


Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()


Quote:
>I don't understand how to use int getchar() properly.
>How do I test if anything is in the stdin stream?  If I
>do a call to getchar() and there's nothing in stdin, then
>execution stops and the program waits for keyboard input.
>On the other hand if I assume nothing is in stdin, then
>my assigning the return value of getchar to some variable
>may pick up trash that was left over from some previous
>function call.

>So,
>1) How can I test if anything is in the stdin stream?

You can't. Reading stdin is blocking.

Quote:
>--- and/or ---
>2) How do I flush stdin without testing it?

read stdin with getchar() until you received a '\n' or an EOF

Quote:
>   fflush(stdin) is undefined, right?

Indeed.

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Show us your source code, and let us, like
rapacious vultures half-crazed by starvation, rip it to shreds until
there's practically nothing left" --Richard Heathfield CLC



Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()

Quote:

>I don't understand how to use int getchar() properly.
>How do I test if anything is in the stdin stream?  If I
>do a call to getchar() and there's nothing in stdin, then
>execution stops and the program waits for keyboard input.
>On the other hand if I assume nothing is in stdin, then
>my assigning the return value of getchar to some variable
>may pick up trash that was left over from some previous
>function call.

>So,
>1) How can I test if anything is in the stdin stream?
>--- and/or ---
>2) How do I flush stdin without testing it?
>   fflush(stdin) is undefined, right?

These questions have been asked here frequently, and continue to be asked
here frequently.  Many frequently-asked questions, and their answers, can be
found in the comp.lang.c FAQ, available at

http://www.eskimo.com/~scs/C-faq/top.html

Your question 1), for example, is question 19.2 in the FAQ, at
http://www.eskimo.com/~scs/C-faq/q19.2.html

but I highly recommend reading the entire list of questions and answers.
You'll be particularly interested in sections 12 (Stdio) and 19 (System
dependencies)
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies



Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()

Quote:
> These questions have been asked here frequently, and continue to be
asked
> here frequently.  Many frequently-asked questions, and their answers,
can be
> found in the comp.lang.c FAQ, available at

> http://www.eskimo.com/~scs/C-faq/top.html

> Your question 1), for example, is question 19.2 in the FAQ, at
> http://www.eskimo.com/~scs/C-faq/q19.2.html

> but I highly recommend reading the entire list of questions and
answers.
> You'll be particularly interested in sections 12 (Stdio) and 19
(System
> dependencies)

Thank you for directing me straight to the answer in the FAQ (as
disappointing as it is).  Surely there's some resourceful soul out
there who's found a way around this "blocking" dilemma.  How about
doing an ungetc (which I understand is guaranteed to work at least
one time) and then looping until you encounter a '\n' or EOF.  Any
pitfalls, there?  I will study sections 12 & 19 as you suggest.

Thanks,
Bob

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()
Please don't bother telling me how stupid my thought about using
ungetc was.  I figured out for myself just how bone-headed that one
was.

Bob

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 02 Nov 2002 03:00:00 GMT  
 Heissenberg's getchar()

Quote:

>> These questions have been asked here frequently, and continue to be
>asked
>> here frequently.  Many frequently-asked questions, and their answers,
>can be
>> found in the comp.lang.c FAQ, available at

>> http://www.eskimo.com/~scs/C-faq/top.html

>> Your question 1), for example, is question 19.2 in the FAQ, at
>> http://www.eskimo.com/~scs/C-faq/q19.2.html

>> but I highly recommend reading the entire list of questions and
>answers.
>> You'll be particularly interested in sections 12 (Stdio) and 19
>(System
>> dependencies)

>Thank you for directing me straight to the answer in the FAQ (as
>disappointing as it is).  Surely there's some resourceful soul out
>there who's found a way around this "blocking" dilemma.  

It's platform specific, without knowing your HW arch. and OS
how can one say? For example, in DOS it was _kbhit() in Win32
it's PeekConsoleInput(s_hConIn, &peek, 1, &peekRecordsRead).
Linux, different. etc.


Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. while (getchar() !=\'n');

2. getchar doesn't wait for prompt

3. multiple getchar()'s in gcc (weird)

4. 'short' data type and getchar()?

5. Question: getchar() one char w/o '\n' ..?

6. int getchar(); or char getchar();

7. Using getchar() instead of fgets or scanf

8. EOF, getchar() and preprocessor portability issues (Re: O

9. Getchar and loop

10. getchar and 256 char hangup

11. defaulting with getchar problem

12. getch vs getchar vs getc

 

 
Powered by phpBB® Forum Software