例如,用户可以输入 l
s 或 ls 命令
。此命令可能不会每次都出现。如何检查用户是否输入了命令?在 scanf()
的情况下,如果命令存在,程序运行正常,但如果命令不存在,那么它将等待输入。我不希望程序等待这个输入
最佳答案
select可用于此目的。因此,您可以在执行(阻塞)读取之前查询 stdin(超时或非阻塞),如果有要读取的内容。
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without blocking.
关于c - C语言有没有不用等待输入就可以检查用户是否输入的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12517198/