SIGINT 处理和 getline
我编写了这个简单的程序:
void sig_ha(int signum)
{
cout<<"received SIGINT\n";
}
int main()
{
string name;
struct sigaction newact, old;
newact.sa_handler = sig_ha;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGINT,&newact,&old);
for (int i=0;i<5;i++)
{
cout<<"Enter text: ";
getline(cin,name);
if (name!="")
cout<<"Text entered: "<<name;
cout<<endl;
}
return 0;
}
如果在程序等待输入时按下 Ctrl+C,我会得到以下输出:
输入文本:收到 SIGINT
输入文本:
输入文字:
输入文字:
输入文字:(
程序继续循环,不等待输入)
我该怎么办?
I wrote this simple program:
void sig_ha(int signum)
{
cout<<"received SIGINT\n";
}
int main()
{
string name;
struct sigaction newact, old;
newact.sa_handler = sig_ha;
sigemptyset(&newact.sa_mask);
newact.sa_flags = 0;
sigaction(SIGINT,&newact,&old);
for (int i=0;i<5;i++)
{
cout<<"Enter text: ";
getline(cin,name);
if (name!="")
cout<<"Text entered: "<<name;
cout<<endl;
}
return 0;
}
If I hit Ctrl+C while the program waits for input I get the following output:
Enter text: received SIGINT
Enter text:
Enter text:
Enter text:
Enter text:
(the program continues the loop without waiting for input)
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
cout
语句之前添加以下内容:Try adding the following immediately before your
cout
statement: