Stream使函数永远运行

发布于 2024-09-16 05:32:44 字数 40 浏览 3 评论 0原文

什么样的流可以导致函数永远运行?

这样的流存在吗?

What kind of stream can cause to function run forever ?

Is such stream exist ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

表情可笑 2024-09-23 05:32:44

标准输入。

The standard input.

霓裳挽歌倾城醉 2024-09-23 05:32:44

ifstream(“/dev/随机”)
ifstream(“/dev/zero”)

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

寒江雪… 2024-09-23 05:32:44

如果流遇到错误,它将停止返回/接受信息。如果您的函数正在等待某些内容从流中出来,它将永远旋转。

使用 if ( cin ) (将流转换为 bool)来测试错误情况。或者,在程序或流初始化时调用 cin.exceptions( ios::badbit ) ,以便错误条件引发异常,而不是安静地旋转。

调用 cin.clear() (或任何流),然后调用 cin.ignore() 以删除有问题的输入(如果程序能够从此类错误中恢复)错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文