忽略从标准输入退格键 [英] Ignore backspace key from stdin

查看:176
本文介绍了忽略从标准输入退格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个程序,迫使它是用户输入的文本,但不允许他删除任何信息,有什么用C做的一个简单的方法?

I want to make a program that forces it's user to input text but doesn't allow him to erase any of it, what's a simple way of doing it in C?

我已经得到的唯一的事情就是(C =的getchar())= EOF和放大器;!&安培; !C ='\\ B'不工作。任何想法?

The only thing I've got is (c = getchar()) != EOF && c != '\b' which doesn't work. Any ideas?

推荐答案

POSIX - Unix版本的

POSIX - unix version

#include <sys/types.h>        
#include <termios.h>
#include <stdio.h>
#include <string.h>

int
 main()
{

         int fd=fileno(stdin);
         struct termios oldtio,newtio;
         tcgetattr(fd,&oldtio); /* save current settings */
         memcpy(&newtio, &oldtio, sizeof(oldtio));
         newtio.c_lflag = ICANON;
         newtio.c_cc[VERASE]   = 0;     /* turn off del */
         tcflush(fd, TCIFLUSH);
         tcsetattr(fd,TCSANOW,&newtio);
         /* process user input here */

         tcsetattr(fd,TCSANOW,&oldtio);  /* restore setting */
         return 0;        
}

这篇关于忽略从标准输入退格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆