Ctrl + C interrupt event handling in Linux

前端 未结 3 1825
臣服心动
臣服心动 2020-11-27 04:44

I am developing an application that uses C++ and compiles using Linux GNU C Compiler.

I want to invoke a function as the user interrupts the script using Ctrl<

3条回答
  •  抹茶落季
    2020-11-27 05:05

    You can use the signal macro.

    Here is an example of how to deal with it:

    #include 
    #include 
    void sigint(int a)
    {
        printf("^C caught\n");
    }
    int main()
    {
        signal(SIGINT, sigint);
        for (;;) {}
    }
    

    Sample output:

    Ethans-MacBook-Pro:~ phyrrus9$ ./a.out
    ^C^C caught
    ^C^C caught
    ^C^C caught
    ^C^C caught
    ^C^C caught
    

提交回复
热议问题