现在的位置: 首页 > 综合 > 正文

在linux中使用getch()函数

2018年02月09日 ⁄ 综合 ⁄ 共 557字 ⁄ 字号 评论关闭

由于在linux中没有conio.h文件,所以不能直接用getch()函数,下面介绍如何在linux中使用getch()函数:

在linux中并没有 conio.h 这个文件,要实现类似 getch()/getche() 等函数的功能,可以使用 curses库。

#include <curses.h>
使用 curses 之前要先进行初始化,用完了要注消————这些操作分别调用 initscr() endwin() 来完成.

main(){
initscr();
.
.
.
endwin();
}

注:在编译的时候如果编译不过,可以试着添加 -lcurses 参数来引入 curses 库

 

例如:

1.建立test.c 文件

#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include <curses.h>
int main()
{
   initscr();
    char ch;
    int i;
    while(1){
        ch=getch();
        printf("%c",ch);
        fflush(stdout);
    }
    endwin();
    return 0;

}

 

2.用以下命令编译:gcc -o test -lcurses test.c

3.运行:./test    即可看到效果

抱歉!评论已关闭.