How to port this program from conio to curses?

I wrote this simple program on Windows. Since Windows has conio, it worked perfectly.

#include <stdio.h> #include <conio.h> int main() { char input; for(;;) { if(kbhit()) { input = getch(); printf("%c", input); } } } 

Now I want to port it to Linux, and curses / ncurses looks like the right way to do this. How can I do the same using these libraries instead of conio?

+7
source share
2 answers
 #include <stdio.h> #include <ncurses.h> int main(int argc, char *argv) { char input; initscr(); // entering ncurses mode raw(); // CTRL-C and others do not generate signals noecho(); // pressed symbols wont be printed to screen cbreak(); // disable line buffering while (1) { erase(); mvprintw(1,0, "Enter symbol, please"); input = getch(); mvprintw(2,0, "You have entered %c", input); getch(); // press any key to continue } endwin(); // leaving ncurses mode return 0; } 

When creating your program, remember to link the ncurses lib (-L lncurses) flag with gcc

 gcc -g -o sample sample.c -L lncurses 

And here you can see the kbhit () implementation for linux.

+9
source

Install ncurses and just include <ncurses.h> .

this will be useful for installing ncurses.

0
source

All Articles