如何实现getline()的超时? [英] How to implement timeout for getline()?

查看:103
本文介绍了如何实现getline()的超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过c ++中的 getline()从命令行读取字符串。

I want to read a string from command line through getline() in c++.

为此,我想添加一个 5 秒的计时器。如果未读取任何字符串,则程序将终止。

For that I want to add a timer of 5 sec. If no string read, then the program will terminate.

我该怎么做?

推荐答案

如何:

/* Wait 5 seconds. */
alarm(5);

/* getline */

/* Cancel alarm. */
alarm(0);

或者,您可以使用 setitimer

R。 Martinho Fernandes 请求:

函数 alarm 安排当前过程在5中接收SIGALRM通话后的秒数。 SIGALRM si的默认操作是异常终止该进程。调用 alarm(0)会禁用计时器。

The function alarm arranges for the current process to receive a SIGALRM in 5 seconds from its call. The default action for a SIGALRM si to terminate the process abnormally. Calling alarm(0) disables the timer.

这篇关于如何实现getline()的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆