2012-09-03 25 views
7

我在Windows上編寫了這個簡單的程序。由於Windows有conio,它工作得很好。如何將此程序從conio移植到curses?

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

int main() 
{ 
    char input; 

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

現在我想將它移植到Linux,curses/ncurses似乎是正確的方式來做到這一點。我將如何使用這些庫代替conio來實現相同的功能?

回答

9
#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你可以看到針對Linux的kbhit()實現。

+0

謝謝,這正是我需要的。 –

+0

您隨時歡迎。 –

0

安裝ncurses並只包含<ncurses.h>

安裝ncurses this將會很有幫助。

+0

kbhit()似乎不存在,或者我做錯了什麼? –

+0

我不確定kbhit()在ncurses中實現。 – Jeyaram