c语言中getche的用法,c语言中getch的用法

【www.hywsbj.com--图表展板】

getch()函数是无回显的从控制台取得一个字符。以利用getch()函数让程序调试运行结束后等待编程者按下键盘才返回编辑界面,即任意键继续效果。下面小编就跟你们详细介绍下c语言中getch的用法,希望对你们有用。

qr.oh100

c语言中getch的用法如下:

[cpp] view plain copy

#include

#include

int main()

{

int i ;

i = getch();

printf("press any key to continue

");

printf("%d

", i);

return 0;

}

Windows下getch()在conio.h的头文件中,但conio.h不是标准库文件,C standard library,ISO C 和POSIX标准中均没有定义。固然Linux系统中会没有这个头文件,网上说在curses.h,然后下载一个库,但弄了半天也没成功取得,从网上找到了一个方法实现getch()的功能。

[cpp] view plain copy

int getch()

{

struct termios tm, tm_old;

int fd = STDIN_FILENO,c;

if (tcgetattr(fd, &tm) < 0)

{

return -1;

}

tm_old = tm;

cfmakeraw(&tm);

if (tcsetattr(fd, TCSANOW, &tm) < 0)

{

return -1;

}

c = fgetc(stdin);

if (tcsetattr(fd,TCSANOW,&tm_old) < 0)

{

return -1;

}

return c;

}

直接可以这样用:

[cpp] view plain copy

/******************************************************************

*描述: 实现任意键继续

*参数: void

*返回值: void

*******************************************************************/

void press_key()

{

printf("任意键继续...

");

getch();

}

头文件

[cpp] view plain copy

#include

#include

#include

#include

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值