Stopping getline in C

夙愿已清 提交于 2019-12-06 06:06:47

Assuming your code resembles this:

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

Include <signal.h> and bind SIGINT to a handler function 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

    }
}

What I would do at this point is restructure the code so that any code after point B is in its own function, call it code_in_b(), and in the handler call code_in_b().

More information: Signals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!