使用 Ctrl+C 中的 SIGINT
好吧,所以我使用叹息控制器来解释一些信号,为此目的它是 Ctrl+C,所以当 Ctrl+C 将采取一些操作,一切都很好,但我真正需要的是在输入/输出中不出现 ^C
的情况下发生这种情况,
例如让我们假设我有这段代码,
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void siginthandler(int param)
{
printf("User pressed Ctrl+C\n");
exit(1);
}
int main()
{
signal(SIGINT, siginthandler);
while(1);
return 0;
}
输出将是
^CUser Pressed Ctrl+C
我怎样才能让它简单地是
User Pressed Ctrl+C
?
alright, so i'm using a sighandler to interpret some signal, for this purpose it is Ctrl+C, so when Ctrl+C is typed some action will be taken, and everything is fine and dandy, but what I really need is for this to happen without ^C
appearing in the input/output
for example let's say i have this code
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void siginthandler(int param)
{
printf("User pressed Ctrl+C\n");
exit(1);
}
int main()
{
signal(SIGINT, siginthandler);
while(1);
return 0;
}
the output would be
^CUser pressed Ctrl+C
How could I get this to simply be
User pressed Ctrl+C
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用curses库的“noecho()”调用来实现这种效果——但您也必须愿意接受(和/或使用)curses终端处理...
You can achieve this effect using the curses library's "noecho()" call -- but you must be willing to accept (and/or use) curses terminal handling too ...
那很可能不是你的程序这样做的。它可能是终端驱动程序,在这种情况下,您应该使用
ctlecho
或echoctl
选项查看stty
。您可以使用 stty -a 获取终端设备的所有当前设置。对于这些项目的编程接口,
termios
就是这样去。That is most likely not your program doing that. It's likely to be the terminal drivers, in which case you should look into
stty
with thectlecho
orechoctl
options.You can use
stty -a
to get all the current settings for your terminal device. For a programmatic interface to these items,termios
is the way to go.正如其他人所说,这种事情由终端驱动程序控制,通常是使用curses库完成的。也就是说,这里有一个可能可以帮助您上路的技巧:
As other have said, this kind of thing falls under the control of your terminal driver and is typically done using the curses library. That said, here is a hack that will probably get you on your way: