How do I port this program from conio to curses?

佐手、 提交于 2020-01-22 19:52:10

问题


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

#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 seems like the right way to do it. How would I accomplish the same using those libraries in place of conio?


回答1:


#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 building your program do not forget to link with ncurses lib (-L lncurses) flag to gcc

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

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




回答2:


Install the ncurses and Just include <ncurses.h>.

for Installing ncurses this will be help-ful.



来源:https://stackoverflow.com/questions/12247674/how-do-i-port-this-program-from-conio-to-curses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!