忽略标准输入中的退格键
我想制作一个程序,强制用户输入文本,但不允许他删除任何内容,在 C 中执行此操作的简单方法是什么?
我唯一拥有的是 (c = getchar()) != EOF && c != '\b'
不起作用。有什么想法吗?
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?
The only thing I've got is (c = getchar()) != EOF && c != '\b'
which doesn't work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用可移植代码来做到这一点 - 本质上每个操作系统都在标准输入流中内置了某种最小缓冲/编辑功能。
根据您需要定位的操作系统,有一个很好的更改,您将拥有一个可以执行无缓冲读取的
getch
。在 Windows 上,您可以包含
并继续使用。在大多数 Unix 上,您需要包含(并链接到)curses(或 ncurses)。You can't do it with portable code -- essentially every OS has some sort of minimal buffering/editing built into the standard input stream.
Depending on the OSes you need to target, there's a good change you'll have a
getch
available that will do unbuffered reading. On Windows, you include<conio.h>
and go for it. On most Unix, you'll need to include (and link to) curses (or ncurses) for it.这可能比您想象的更复杂。为此,您可能需要接管对用户正在键入的字符的回显等的控制。
请查看curses 库。 wgetch 函数应该是您所需要的,但首先您需要初始化curses 等。阅读手册页 - 如果幸运的话,您会找到ncurses 或curses-intro 手册页。这是一个片段:
如果您没有该手册页/了解更多信息,请查找各个函数的手册页。
This is likely more complicated than you imagine. To do this, you'll presumably need to take over control of echoing the characters the user is typing etc.
Have a look at the curses library. The wgetch function should be what you need, but first you'll need to initialise curses etc. Have a read of the man pages - if you're lucky you'll find ncurses or curses-intro man pages. Here's a snippet:
If you've not got that manpage / for further info, look up the individual function man pages.
POSIX-unix 版本
POSIX - unix version