代码之家  ›  专栏  ›  技术社区  ›  Iceland_jack

忽略标准输入中的退格键

  •  5
  • Iceland_jack  · 技术社区  · 16 年前

    我唯一能做的就是 (c = getchar()) != EOF && c != '\b' 这是行不通的。有什么想法吗?

    3 回复  |  直到 16 年前
        1
  •  5
  •   jim mcnamara    16 年前

    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;        
    }
    
        2
  •  3
  •   Jerry Coffin    16 年前

    你不能用可移植的代码来实现这一点——基本上每个操作系统都在标准的输入流中内置了一些最小的缓冲/编辑功能。

    根据你需要瞄准的操作系统,你会有一个很好的改变 getch 可用于无缓冲读取。在Windows上,包括 <conio.h>

        3
  •  2
  •   asc99c    16 年前

       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);
    

    如果您还没有该手册页/更多信息,请查看各个功能手册页。