【问题标题】:How do I port this program from conio to curses?如何将此程序从 conio 移植到 curses?
【发布时间】:2012-08-28 04:19:05
【问题描述】:

我在 Windows 上编写了这个简单的程序。由于 Windows 有 conio,所以它工作得很好。

#include <stdio.h>
#include <conio.h>

int main()
{
    char input;

    for(;;)
    {
        if(kbhit())
        {
            input = getch();
            printf("%c", input);
        }
    }
}    

现在我想将它移植到 Linux,curses/ncurses 似乎是正确的方法。我将如何使用这些库代替 conio 来完成同样的任务?

【问题讨论】:

    标签: c linux ncurses curses 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;
    }
    

    在构建程序时不要忘记将 ncurses lib (-L lncurses) 标志链接到 gcc

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

    here 你可以看到 kbhit() 在 linux 上的实现。

    【讨论】:

    • 谢谢,这正是我所需要的。
    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多