views:

60

answers:

3

Hi,

What kind of stream can cause to function run forever ?

Is such stream exist ?

+3  A: 

The standard input.

xtofl
+2  A: 

ifstream("/dev/random") ifstream("/dev/zero")

Tony
+2  A: 

If a stream encounters an error, it will stop returning/accepting information. If your function is waiting for something to come out of the stream, it will spin forever.

Use if ( cin ) (cast the stream to bool) to test for an error condition. Alternately, call cin.exceptions( ios::badbit ) at program or stream initialization so an error condition throws an exception rather than quietly spinning.

Call cin.clear() (or whatever stream) followed by cin.ignore() such as to remove the offending input, if the program is able to recover from such an error.

Potatoswatter
Nope, nope... that answer's way too serious. And the question was "what kind of stream", not what stream state. +1 from me anyway ;-)
Tony