Click to See Complete Forum and Search --> : detecting if stream refers to standard input


Krist
03-28-2001, 05:55 AM
I'm trying to detect if a stream is getting its stadnard input from the console
(i.e. if it is waiting indefinitely for a user to type something) in a console
application. Then, I can use this detection to generate some error.
To achieve this, I used the following function

istream myStream(cin);

streamsize charsAvailable = myStream.rdbuf()->in_avail()

if this call returns a result > 0, then there are characters available in
the input buffer.
unfortunately, if the result of this call is 0, one is not guaranteed that
cin.get() will NOT have a char available

Does anyone know a better solution (using the istream object ?)

Sam
03-28-2001, 06:16 AM
Hi,
What i feel that u want is to get the input from
the Keyboard and wan't to manipulate the same.
I hope the kbhit function can be used for the
same easily.

Sam.
"Krist" <krist.blomme@ieee.org> wrote:
>
>I'm trying to detect if a stream is getting its stadnard input from the
console
>(i.e. if it is waiting indefinitely for a user to type something) in a console
>application. Then, I can use this detection to generate some error.
>To achieve this, I used the following function
>
>istream myStream(cin);
>
>streamsize charsAvailable = myStream.rdbuf()->in_avail()
>
>if this call returns a result > 0, then there are characters available in
>the input buffer.
>unfortunately, if the result of this call is 0, one is not guaranteed that
>cin.get() will NOT have a char available
>
>Does anyone know a better solution (using the istream object ?)

Krist
03-28-2001, 07:26 AM
"Sam" <devhelp@rediffmail.com> wrote:
>
>Hi,
>What i feel that u want is to get the input from
>the Keyboard and wan't to manipulate the same.
>I hope the kbhit function can be used for the
>same easily.
>
>Sam.

In my program, input is read from a general stream. This stream can be standard
input, a file or another stream. Normally the user doesn't want it to be
standard input, because he doesn't want to type the input manually. So what
I want is to detect if a general stream happens to be standard input from
the console, so that I can inform the user that he propably doesn't want
that.