忽略标准输入中的退格键

发布于 2024-09-08 00:32:25 字数 142 浏览 5 评论 0原文

我想制作一个程序,强制用户输入文本,但不允许他删除任何内容,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李白 2024-09-15 00:32:26

您无法使用可移植代码来做到这一点 - 本质上每个操作系统都在标准输入流中内置了某种最小缓冲/编辑功能。

根据您需要定位的操作系统,有一个很好的更改,您将拥有一个可以执行无缓冲读取的 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.

可是我不能没有你 2024-09-15 00:32:26

这可能比您想象的更复杂。为此,您可能需要接管对用户正在键入的字符的回显等的控制。

请查看curses 库。 wgetch 函数应该是您所需要的,但首先您需要初始化curses 等。阅读手册页 - 如果幸运的话,您会找到ncurses 或curses-intro 手册页。这是一个片段:

   To  initialize  the  routines,  the  routine initscr or newterm must be
   called before any of the other routines  that  deal  with  windows  and
   screens  are  used.   The routine endwin must be called before exiting.
   To get character-at-a-time input  without  echoing  (most  interactive,
   screen  oriented  programs want this), the following sequence should be
   used:

         initscr(); cbreak(); noecho();

   Most programs would additionally use the sequence:

         nonl();
         intrflush(stdscr, FALSE);
         keypad(stdscr, TRUE);

如果您没有该手册页/了解更多信息,请查找各个函数的手册页。

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:

   To  initialize  the  routines,  the  routine initscr or newterm must be
   called before any of the other routines  that  deal  with  windows  and
   screens  are  used.   The routine endwin must be called before exiting.
   To get character-at-a-time input  without  echoing  (most  interactive,
   screen  oriented  programs want this), the following sequence should be
   used:

         initscr(); cbreak(); noecho();

   Most programs would additionally use the sequence:

         nonl();
         intrflush(stdscr, FALSE);
         keypad(stdscr, TRUE);

If you've not got that manpage / for further info, look up the individual function man pages.

你与昨日 2024-09-15 00:32:25

POSIX-unix 版本

#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;        
}

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;        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文