Linux练习(读取字符立即回显)

#include 
#include 
int main()
{
        char s;
//        FILE *in;
//        FILE *out;
        struct termios initial_settings,new_settings;
//        in=fopen("/dev/tty","r");
//        out=fopen("/dev/tty","w");

        tcgetattr(fileno(stdin),&initial_settings);//保存原来的设置
        new_settings=initial_settings;    //开始新终端控制设置
        new_settings.c_lflag &= ~ICANON;
        new_settings.c_lflag &= ~ECHO;
        new_settings.c_cc[VMIN] = 1;
        new_settings.c_cc[VTIME] = 0;
        if(tcsetattr(fileno(stdin),TCSANOW,&new_settings)!=0) //应用新的设置
                fprintf(stderr,"coutld not set attributes\n");

        do
        {
            s=fgetc(stdin);
            fprintf(stdout,"\nyou input %c\n",s);
        }while(s!='q');
        
        tcsetattr(fileno(stdin),TCSANOW,&initial_settings);//返回原来的设置
                
        return 0;
}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/09/09/2677910.html

你可能感兴趣的:(Linux练习(读取字符立即回显))