在 C 中捕获 Ctrl-C
如何在 C 中捕捉 Ctrl+C ?
How does one catch Ctrl+C in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 C 中捕捉 Ctrl+C ?
How does one catch Ctrl+C in C?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
使用信号处理程序。
下面是一个翻转
main()
中使用的bool
的简单示例:2017 年 6 月编辑:它可能涉及的对象,特别是那些拥有编辑这个答案的强烈冲动。看,我七年前写了这个答案。是的,语言标准发生了变化。如果您确实必须让世界变得更好,请添加您的新答案,但保留我的原样。由于答案上有我的名字,我希望它也包含我的话。谢谢。
With a signal handler.
Here is a simple example flipping a
bool
used inmain()
:Edit in June 2017: To whom it may concern, particularly those with an insatiable urge to edit this answer. Look, I wrote this answer seven years ago. Yes, language standards change. If you really must better the world, please add your new answer but leave mine as is. As the answer has my name on it, I'd prefer it to contain my words too. Thank you.
检查此处:
注意:显然,这是一个简单的示例,只是解释如何设置Ctrl+C 处理程序,但一如既往,需要遵守一些规则,以免破坏其他内容。请阅读下面的评论。
上面的示例代码:
Check here:
Note: Obviously, this is a simple example explaining just how to set up a Ctrl+C handler, but as always there are rules that need to be obeyed in order not to break something else. Please read the comments below.
The sample code from above:
关于 UN*X 平台的附录。
根据 GNU/Linux 上的
signal(2)
手册页,signal
的行为不如sigaction
的行为可移植:在系统 V 上,系统不会阻止进一步发送信号实例,并且发送信号会将处理程序重置为默认处理程序。在 BSD 中,语义发生了变化。
Dirk Eddelbuettel 之前的答案的以下变体使用
sigaction
而不是signal
:Addendum regarding UN*X platforms.
According to the
signal(2)
man page on GNU/Linux, the behavior ofsignal
is not as portable as behavior ofsigaction
:On System V, system did not block delivery of further instances of the signal and delivery of a signal would reset the handler to the default one. In BSD the semantics changed.
The following variation of previous answer by Dirk Eddelbuettel uses
sigaction
instead ofsignal
:@Peter Varo 更新了德克的答案,但德克拒绝了更改。这是彼得的新答案:
虽然上面的代码片段是正确的 c89 例如,如果可能的话,应该使用后来的标准提供的更现代的类型和保证。因此,对于那些正在寻找 c99 和 c11 符合实施:
此外:
@Peter Varo updated Dirk's answer, but Dirk rejected the change. Here's the new answer by Peter:
Although the above snippet is a correct c89 example, one should use the more modern types and guarantees provided by the later standards if possible. Therefore, here is a safer and modern alternative for those who are seeking for the c99 and c11 conforming implementation:
Furthermore:
或者您可以将终端置于原始模式,如下所示:
现在应该可以使用
fgetc(stdin)
读取 Ctrl+C 击键。但要小心使用它,因为你不能 Ctrl+Z、Ctrl+Q、Ctrl< /kbd>+S 等也像平常一样。Or you can put the terminal in raw mode, like this:
Now it should be possible to read Ctrl+C keystrokes using
fgetc(stdin)
. Beware using this though because you can't Ctrl+Z, Ctrl+Q, Ctrl+S, etc. like normally any more either.设置陷阱(您可以使用一个处理程序捕获多个信号):
按照您想要的方式处理信号,但要注意限制和陷阱:
Set up a trap (you can trap several signals with one handler):
Handle the signal however you want, but be aware of limitations and gotchas:
关于现有答案,请注意信号处理取决于平台。例如,Win32 处理的信号比 POSIX 操作系统少得多; 请参阅此处。虽然 SIGINT 在 Win32 上的 Signals.h 中声明,但请参阅文档中的注释,说明它不会执行您可能期望的操作。
Regarding existing answers, note that signal handling is platform dependent. Win32 for example handles far fewer signals than POSIX operating systems; see here. While SIGINT is declared in signals.h on Win32, see the note in the documentation that explains that it will not do what you might expect.
函数 sig_handler 检查传递的参数值是否等于 SIGINT,然后执行 printf。
The function sig_handler checks if the value of the argument passed is equal to the SIGINT, then the printf is executed.
这只是在退出之前打印。
This just print before exit.