为getchar()添加超时 [英] Add a Timeout for getchar()

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

问题描述

我需要在程序中为getchar()添加一个超时函数.

I need to add a timeout function for getchar() in my program.

我该怎么做,以便当我的程序到达指令getchar()时,它将仅等待特定时间,以便用户进行击键,并且如果用户未在给定的时间限制内进行击键,程序会跳过" getchar()吗?

What do I do so that when my program reaches the instruction getchar(), it will only wait for a certain amount of time for the user to make a keystroke and if the user does not make a keystroke within the given time limit, the program will "skip" the getchar()?

操作系统不支持conio.h库,因此不能选择kbhit.

The operating system does not support the conio.h library so kbhit is not an option.

推荐答案

通常通过在stdin上使用select()来实现.另一种解决方案是使用alarm()和虚拟SIGALRM处理程序来中断getchar()调用(尽管仅适用于POSIX系统).

This is usually achieved by using select() on stdin. Another solution would be using alarm() and a dummy SIGALRM handler to interrupt the getchar() call (only working on POSIX systems though).

这篇关于为getchar()添加超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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