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

来源:互联网 发布:微信秒杀软件. 编辑:程序博客网 时间:2024/04/28 22:44


#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;
}
0 0
原创粉丝点击