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?
Assuming your code resembles this:
Include
<signal.h>
and bindSIGINT
to a handler functionf
.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 callcode_in_b()
.More information: Signals