用C停止函数getline [英] Stopping getline in C

查看:193
本文介绍了用C停止函数getline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我试图让与函数getline 的用户输入。在接收到中断 ^ C 我想它的信号函数getline停下来与我的程序恢复终止它,而不是。

Here I'm trying to get an user input with getline. Upon receiving an interrupt ^C I want it to signal getline to stop and resume with my program instead of terminating it.

我试着写一个换行符标准输入,但显然不起作用。

I tried to write a newline to stdin but apparently that doesn't work.

fwrite("\n", 1, 1, stdin);

所以,这将是一个方式来实现这一目标?

So what would be a way to achieve this?

推荐答案

假设你的code类似于此:

Assuming your code resembles this:

int main(int argc, char **argv) {
    //Code here (point A)
    getline(lineptr, size, fpt);
    //More code here (point B)
}

包含< signal.h中> 和绑定 SIGINT 来处理函数˚F

#include <signal.h>

//Declare handler for signals
void signal_handler(int signum);

int main(int argc, char **argv) {
    //Set SIGINT (ctrl-c) to call signal handler
    signal(SIGINT, signal_handler);

    //Code here (point A)
    getline(lineptr, size, fpt);
    //More code here (point B)
}

void signal_handler(int signum) {
    if(signum == SIGINT) {
        //Received SIGINT

    }
}

我想在这一点上做的是重组的code,使B点之后的任何code是在它自己的功能,称之为 code_in_b(),并在处理程序调用 code_in_b()

更多信息:信号

这篇关于用C停止函数getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆