您的位置:首页 > 编程语言

从标准输入里面不需要按回车得到一个输入字符 (C代码)

2014-02-26 15:32 302 查看
#include <termios.h>

#include <unistd.h>

#include <stdio.h>

int main(void)

{
char c;

    struct termios tTTYState;

 

    //get the terminal state

    tcgetattr(STDIN_FILENO, &tTTYState);

 

    //turn off canonical mode

    tTTYState.c_lflag &= ~ICANON;

    //minimum of number input read.

    tTTYState.c_cc[VMIN] = 1;   /* 有一个数据时就立刻返回 */

    //set the terminal attributes.

    tcsetattr(STDIN_FILENO, TCSANOW, &tTTYState);

while (1)
{
c = fgetc(stdin);  /* 会休眠直到有输入 */
printf("get char : %c\n", c);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言
相关文章推荐