Stop getline in C

Here I am trying to get user input with getline . Having received the ^C interrupt, I want it to signal getline to stop and resume working with my program, and not to stop it.

I tried to write a new line for stdin, but apparently this does not work.

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

So what would be a way to achieve this?

+2
c signals stdin
source share
1 answer

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 the 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 like to do at this moment is to restructure the code so that any code after point B is in its own function, call it code_in_b() and in the call to the code_in_b() handler.

Additional Information: Signals

0
source share

All Articles