问题描述
我想捕捉我在程序的控制+ D信号和写信号处理程序。
我该怎么办呢?
我工作的C和使用Linux系统。
I want to capture the Control+D signal in my program and write a signal handler for it.How can I do that?I am working on C and using a linux system.
推荐答案
正如其他人已经说过,处理控制-D,处理的文件末尾。
As others have already said, to handle Control-D, handle "end of file"s.
控制-D是一块用户,并且您认为标准输入伪文件之间的通信。这并不意味着明确文件结束,但更普遍的刷新我输入到目前为止输入。法拉盛意味着任何阅读()
标准输入调用程序自上次刷新输入的输入长度返回。如果该行不为空,虽然用户没有键入回归,但在输入可用你的程序。如果该行是空的,那么阅读()
返回零,那就是PTED为文件结束间$ P $。
Control-D is a piece of communication between the user and the pseudo-file that you see as stdin. It does not mean specifically "end of file", but more generally "flush the input I typed so far". Flushing means that any read()
call on stdin in your program returns with the length of the input typed since the last flush. If the line is nonempty, the input becomes available to your program although the user did not type "return" yet. If the line is empty, then read()
returns with zero, and that is interpreted as "end of file".
因此,使用控制-D来结束一个程序时,它只能在一行的开始,或者如果你做两次(第一次以红晕,为第二次阅读()
返回零)。
So when using Control-D to end a program, it only works at the beginning of a line, or if you do it twice (first time to flush, second time for read()
to return zero).
试试吧:
$ cat
foo
(type Control-D once)
foofoo (read has returned "foo")
(type Control-D again)
$
这篇关于如何捕获控制+ D信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!