工作这个程序CTRL-C处理程序无法正常
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <windows.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
void INThandler(int);
int main(void)
{
signal(SIGINT, INThandler);
char data[128];
int n;
while((n=read(0, data, 128)) > 0)
{
if(data[n] = '\n') break;
}
data[n] ='\0';
printf("%s", data);
return 0;
}
void INThandler(int sig)
{
char c;
signal(sig, SIG_IGN);
printf("OUCH, did you hit Ctrl-C?\n"
"Do you really want to quit? [y/n] ");
c = getchar();
if (c == 'y' || c == 'Y')
exit(0);
else
signal(SIGINT, INThandler);
}
无法处理CTRL-C,但在那输入终止。 如果我更换由
while (1)
Sleep(1);
的处理函数被调用和工程处理程序安装和收益之间的一切,但我想有阅读()在那里。
编辑:这个节目回首,我发现我有
if(data[n] = '\n') break;
我写了“=”而不是“==”,但使用后,它不能正确和我的工作不明白为什么。它不应该是检测'\ n'的比较吗? 另外,我搞砸了缓冲区,但我不能保持输入,如果我打CTRL-C。
我明白了,所以它取决于SO。到目前为止,我没有使用Windows的问题,然后在Unix上进行测试。谢谢。 – Aleister 2013-03-06 13:35:47