本文介绍了如何将该程序从conio移植到curses?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Windows上编写了这个简单的程序.由于Windows具有conio,因此效果很好.
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);
}
}
}
现在,我想将其移植到Linux,而curses/ncurses似乎是正确的方法.我将如何使用这些库代替conio来完成相同的工作?
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?
推荐答案
#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 inccurses)标志链接到gcc
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
并且此处,您可以看到linux的kbhit()实现.
And here you can see kbhit() implementation for linux.
这篇关于如何将该程序从conio移植到curses?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!