943,957 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2485
  • C RSS
May 18th, 2008
0

Clearing input stream

Expand Post »
Hi,

Someone told me to use this code to clear the input buffer..

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

But I fail to understand how it actually works.

For example, there are two newline characters in the buffer
Quote ...
This is my buffer

\n \n
Now getchar() grabs the first '\n', stores it in ch and compares ch with '\n' and EOF to see if the condition is true or not. Like this

  1. (ch != '\n' && ch !=EOF)
  2. This gives
  3.  
  4. (false && true) = false.

So the condition is false, the loop will terminate. What about the second '\n'? Its still in the buffer !!! So is this code for clearing only 1 newline character from the buffer?
Similar Threads
Reputation Points: 15
Solved Threads: 2
Junior Poster in Training
Google Spider is offline Offline
53 posts
since Feb 2008
May 18th, 2008
1

Re: Clearing input stream

>What about the second '\n'?
It doesn't exist. That loop is designed for line-oriented interactive input from stdin. Because a newline is expected to terminate the buffering mechanism for your shell, there will only ever be one newline, and that newline will mark the end of the buffer. As such, the loop isn't a complete solution, but it's close enough if you use it for the purpose it was designed.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 18th, 2008
0

Re: Clearing input stream

OK so there can't be two newline characters in buffer. How about an EOF following the \n?
Quote ...
\n EOF
>As such, the loop isn't a complete solution

What is the possible complete solution?
Reputation Points: 15
Solved Threads: 2
Junior Poster in Training
Google Spider is offline Offline
53 posts
since Feb 2008
May 18th, 2008
1

Re: Clearing input stream

>How about an EOF following the \n?
That won't happen either. When you press the Enter key, the buffered characters in the shell are sent to your program for processing. That means every character up to and including the newline represent a single input transaction. You can't add characters to the transaction after sending it in for processing, so the EOF signal would be a part of the next transaction, making it irrelevant to this transaction.

The two situations you've described won't happen in line-oriented interactive input, but they can happen if stdin is redirected to a file, which is why this isn't a complete solution. However, the "newline stuck in the input stream" problem doesn't seem to be as much of an issue when dealing with files. It's usually a matter of people not fully understanding how streams work and muxing up interactive input.

>What is the possible complete solution?
There isn't one. When more than one interpretation for a stream of characters exists, it's impossible to have a complete solution to any problem because for at least one interpretation, it won't even be a problem.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How do i use EOF in my program ??
Next Thread in C Forum Timeline: Doubly Linked List Doesnt Work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC