How to discard data in stdin 
Author Message
 How to discard data in stdin

Hi,

We are developing a application which use Tcl as interpreter, but encountered
some issues and have to disable our application's stdin and only allow "gets"
and "read" to read data from stdin.
My issue is that:
when use "read", such as "read stdin 3", and input "1234", read can get "123",
but "4\n" are left in stdin queue, so when I use "gets stdin line",
it returns immediately with "$line == 4".
My question is:
How to empty the stdin queue or discard the left data after "read" finish
reading.

Thanks in advance!



Sun, 07 Nov 2004 16:59:55 GMT  
 How to discard data in stdin

| How to empty the stdin queue or discard the left data after "read"
| finish reading.

read stdin 3
=>
string range [gets stdin] 0 2

If you have line-oriented input data, read whole lines from your
input.

R'



Sun, 07 Nov 2004 17:28:14 GMT  
 How to discard data in stdin
On Wed, 22 May 2002 16:59:55 +0800, "David Wang (Wang Xin-lu)"

Quote:

>Hi,

>We are developing a application which use Tcl as interpreter, but encountered
>some issues and have to disable our application's stdin and only allow "gets"
>and "read" to read data from stdin.
>My issue is that:
>when use "read", such as "read stdin 3", and input "1234", read can get "123",
>but "4\n" are left in stdin queue, so when I use "gets stdin line",
>it returns immediately with "$line == 4".

AFAIK, this is the intended (and documented) behaviour.

Quote:
>My question is:
>How to empty the stdin queue or discard the left data after "read" finish
>reading.

You could do a 'read stdin 3' immediately followed by a 'gets stdin
line' and discard 'line' - but I doubt if this would make you happy.
What if the user enters '1\n' - your 'read' will wait for more input.

IMHO, user input is best handled via gets, followed by a sanity check
of the input: what if you expect a number and get "My bonny lives over
the ocean"?

HTH
Helmut



Sun, 07 Nov 2004 17:29:37 GMT  
 How to discard data in stdin

Quote:
> We are developing a application which use Tcl as interpreter, but encountered
> some issues and have to disable our application's stdin and only allow "gets"
> and "read" to read data from stdin.
> My issue is that:
> when use "read", such as "read stdin 3", and input "1234", read can get "123",
> but "4\n" are left in stdin queue, so when I use "gets stdin line",
> it returns immediately with "$line == 4".
> My question is:
> How to empty the stdin queue or discard the left data after "read" finish
> reading.

I guess you would have to define what exactly you mean by

  left data after "read" finish

It sounds like you mean any data up to and including the end of the
line, in which case you could routinely do a

   read stdin $number_of_bytes
   gets stdin

This will work as long as there is always at least the end-of-line
left over after the "read".

  Harald Kirsch

--
----------------+------------------------------------------------------
Harald Kirsch   | kirschh bei lionbioscience punkt com
       *** Please do not send me copies of your posts. ***



Sun, 07 Nov 2004 19:45:53 GMT  
 How to discard data in stdin
Sorry, may be I did not describe my issue clearly.
We need to fix this issue in C or C++ level, say, using Tcl Library.


Mon, 08 Nov 2004 09:09:19 GMT  
 How to discard data in stdin
OK, Thanks to all, we just fixed this issue.
Just like what you said, after "Tcl_ReadObj" we issue "Tcl_GetsObj" to empty
input queue.


Mon, 08 Nov 2004 09:24:58 GMT  
 How to discard data in stdin


:Sorry, may be I did not describe my issue clearly.
:We need to fix this issue in C or C++ level, say, using Tcl Library.

Actually, we need to leave things as they are, for the most part, since
changing it would likely break applications that depend on the current
behavior.

The issue is this - input to an application vis stdin or any other file
descriptor should be treated as either input via read or input via gets.
Mixing them is seldom correct.

Read says "I want the next N characters, with the expectation that I will
return for the N characters after that later"

Gets says "I want any number of characters up to the next newline."

Trying to intermingle two different input philosophies is like trying to
intermingle two different religions - it can be successful with sufficent
work and tolerance of the others opinions...
--
Support Internet Radio <URL: http://saveinternetradio.org/ >
Join Tcl'2002 in Vancouver http://www.tcl.tk/community/tcl2002/

Even if explicitly stated to the contrary, nothing in this posting



Mon, 08 Nov 2004 20:25:29 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Discarding unsent data on tcl socket

2. program and data via stdin

3. Switching stdin from program to data

4. How to read unformatted data from stdin?

5. creating image from stdin data

6. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

7. J: how do I discard leading axes?

8. Discard Duplicates on a Report CDD3.1

9. Testnews: discard

10. warning: discarding old <methodName>?

11. Ruby/Tk: how to discard excess messages

12. Bits discarded in Multiplication

 

 
Powered by phpBB® Forum Software