how to tell if stdin (cin) has input WITHOUT BLOCKING?

how to tell if stdin (cin) has input WITHOUT BLOCKING?

Post by stephan bea » Tue, 20 May 2003 20:01:46



Yo,

i've got this long-standing problem i'm trying to solve:

how can a C or C++ application know if cin has data without blocking? i
understand that there is no 100% portable way to do this, but i don't need
portable (i need Linux ;). i've googled all over for this and haven't found
a single relevant example.

i've looked at other programs, like gzip, to see how they do it, but they
typically simply assume input-from-stdin if no other command-line arguments
are given. My app, on the other hand, should start in interactive mode if
cin is not waiting to give us something, otherwise it should process cin
and exit.

$> myapp
(starts interactively)

$> myapp < foo
should read in foo and skip interactive mode

(To be clear, the only part i'm hung up on is the checking of stdin. i
currently work around this with a --stin flag, which tells the app to read
stdin and then exit.)

Is this a pipe dream?

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

 
 
 

how to tell if stdin (cin) has input WITHOUT BLOCKING?

Post by j.. » Tue, 20 May 2003 21:36:14



> i've got this long-standing problem i'm trying to solve:

> how can a C or C++ application know if cin has data without
> blocking? i understand that there is no 100% portable way to do
> this, but i don't need portable (i need Linux ;). i've googled all
> over for this and haven't found a single relevant example.

> i've looked at other programs, like gzip, to see how they do it, but
> they typically simply assume input-from-stdin if no other
> command-line arguments are given. My app, on the other hand, should
> start in interactive mode if cin is not waiting to give us
> something, otherwise it should process cin and exit.

> $> myapp
> (starts interactively)

> $> myapp < foo
> should read in foo and skip interactive mode

> (To be clear, the only part i'm hung up on is the checking of
> stdin. i currently work around this with a --stin flag, which tells
> the app to read stdin and then exit.)

Sounds like you really want to know if stdin is a terminal, not
whether there's something available. Try isatty(3).

Joe

 
 
 

how to tell if stdin (cin) has input WITHOUT BLOCKING?

Post by stephan bea » Tue, 20 May 2003 22:13:31



> Sounds like you really want to know if stdin is a terminal, not
> whether there's something available. Try isatty(3).

That looks like it will do the trick. Thanks a lot, Joe!

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

 
 
 

how to tell if stdin (cin) has input WITHOUT BLOCKING?

Post by Floyd Davidso » Wed, 21 May 2003 04:53:04



>Yo,

>i've got this long-standing problem i'm trying to solve:

>how can a C or C++ application know if cin has data without blocking? i
>understand that there is no 100% portable way to do this, but i don't need
>portable (i need Linux ;). i've googled all over for this and haven't found
>a single relevant example.

Blocking, or not, isn't what you really want to know.  You want to know
if the input is connected to an interactive device...  either a tty or
a pseudo-tty.

Quote:>i've looked at other programs, like gzip, to see how they do it, but they
>typically simply assume input-from-stdin if no other command-line arguments
>are given. My app, on the other hand, should start in interactive mode if
>cin is not waiting to give us something, otherwise it should process cin
>and exit.

>$> myapp
>(starts interactively)
>$> myapp < foo
>should read in foo and skip interactive mode

  if (isatty(0)) {
     /* will be true if stdin is a terminal */
  } else {
     /* will be false if stdin is connected to a file */
  }

Quote:>(To be clear, the only part i'm hung up on is the checking of stdin. i
>currently work around this with a --stin flag, which tells the app to read
>stdin and then exit.)

>Is this a pipe dream?

"man isatty"

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

how to tell if stdin (cin) has input WITHOUT BLOCKING?

Post by stephan bea » Wed, 21 May 2003 22:45:39



> Sounds like you really want to know if stdin is a terminal, not
> whether there's something available. Try isatty(3).

Hi again!

After confirming that this does the trick, i tried to send you a Thanks
mail, only to find that your address is invalid. So, in case you see this:
THANKS! That solved a problem i've been wondering how to solve for 2 years.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

 
 
 

1. How to tell if there is input waiting at stdin?

Please help me.  I've been pounding my head into the terminal and man
pages most of the day and can't even come up with a kludge to this
problem.

How can you tell if there is input waiting to be read at stdin?  I am
working on a program that accepts commands from the user via stdin but
must do other things if there are no commands (it is X based).

I assume that

  if( !feof(stdin))
    ProcessCommands();

  is the wrong thing to do since stdin will have an end of file only
when the shell is terminated.

I have also tried

  ioctl(0, FIONREAD, &n)
  if(n >= 0)
    ProcessCommands();

  however, the gets() call in ProcessCommands() seems to mess this up.
After a gets() call the ioctl call will return zero bytes to be read
even if there are more lines pending.

Thank You,
Fred
--
-----------------------------------------------------------------

Machine Vision Laboratory                            508-831-5395
Electrical Engineering Department
Worcester Polytechnic Institute
Worcester, MA 01609
-----------------------------------------------------------------

2. benchmarks comparing Sun to HP

3. How to read chars from stdin without having to press <enter>

4. hosts.equiv question/simple

5. C stdin/stdout and C++ cin/cout

6. Missing device interrupts in "vmstat -i"

7. how to peek at stdin status (getchar without blocking)

8. programming tools purify/quantify

9. How do you test stdin for waiting characters without blocking?

10. Newbie question: 'cin' input in debug (KDevelop)

11. Performance statistics: What are "input blocks/output blocks"

12. Apache-Proxyblock.conf -Need to block iGoogle without blocking Google

13. Passing a String with an IFS without truncating