Continue the program until the user types Ctrl + a

I am doing C programming for Linux for the exam. I donโ€™t know how to exit the program when the user presses Ctrl + a (not Ctrl + c) For example, looping until the user presses Ctrl + a Can someone tell me how to check Ctrl + input?

Notes: I use 'gcc' and run the output from './a.out'

Thanks in advance to everyone!

+4
source share
7 answers

Have you been looking for something like this ??? this program will not be stopped, since you press Ctrl + A and Enter.

#include <stdio.h> int main() { char a; while( a!=1 ) //Ascii code for ctrl + A == 1 { a=getchar(); printf("still looping ...\n"); } printf( "HA! You pressed CTRL+A\n" ); return 0; } 

But if you want to terminate your program immediately after pressing ctrl + A (without getting into it after that), you are here:

 #include <stdio.h> #include <ncurses.h> int main() { char a; initscr(); raw(); while( a!=1 ) a=getch(); endwin(); return 0; } 

to compile the second code using GCC, try the following command:

 gcc -o program.o -lncurses program.cpp 
+3
source

Turbo C and other C implementations for Windows had a getch() function call that getch() individual characters from the keyboard; that would do what you want.

In POSIX environments, such as those implemented by gcc-compiled Unix / Linux programs, this functionality does not exist directly.

There is a library called curses that allows C programs to perform full-screen output processing, as well as getch() functions in curses. This may be the easiest answer to your problem. You will need to read the curse documentation and link the header files and libraries to your program.

+3
source

There is special support for Ctrl-C, which is converted to a system signal. If you want your program to stop as soon as another specific key combination is used, it will be much more complicated.

  • you will need to check the standard input of your program, and you will need to set the standard input so that the inputs are not buffered (otherwise you will not see any input until it is confirmed by pressing the "return" button). The last part will be done with an ioctl() call and will not be portable;

  • you will need neither a topic nor a survey, none of which are very pleasant in C.

There are more interesting things in C than in C.

+2
source

catch ctrl-c event

This post answers your question.

+2
source

This will do what you want:

 stty intr ^a ./a.out 

For extra credit, do the equivalent of "stty intr ^ a" using the appropriate library function, for example. thermos man.

+1
source

Someone first sent code that works for me, but I donโ€™t know why he deleted his answer.

Original link: http://www.c.happycodings.com/Gnu-Linux/code18.html

 #include <stdio.h> #include <unistd.h> /* sleep(1) */ #include <signal.h> void ex_program(int sig); int main(void) { (void) signal(SIGINT, ex_program); while(1) printf("sleeping .. ZZZzzzz ....\n"), sleep(1); return 0; } void ex_program(int sig) { printf("Wake up call ... !!! - Catched signal: %d ... !!\n", sig); (void) signal(SIGINT, SIG_DFL); } 
0
source

Here's how to set CTRL-A as an interrupt to interrupt a process. Also note that CTRL-C does not interrupt it after calling tcsetattr .

 #include <stdio.h> #include <termios.h> #include <unistd.h> int main(int argc, char* argv[]) { struct termios termios; if ( tcgetattr(0, &termios) != -1 ) { termios.c_cc[VINTR] = '\x01'; /* CTRL-A */ if ( tcsetattr(0, 0, &termios) != -1 ) { printf("Ready. Press CTRL-A to break this program\n"); while ( 1 ) { printf("*\n"); sleep(1); } } } } 
0
source

All Articles