开发者

Stream makes function run forever [closed]

开发者_运维百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

What kind of stream can cause to function run forever ?

Is such stream exist ?


The standard input.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜