tags:

views:

76

answers:

3

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?

A: 

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.

Jerry Coffin
Wrong about curses. See the accepted answer. It's very easy and portable (POSIX).
R..
@R: I looked at it. I'm not sure it's quite right yet, and curses is definitely a lot easier.
Jerry Coffin
+2  A: 

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;        
}
jim mcnamara
+1. I din't realize that it is possible to disable only some keys in the cooked mode.
Dummy00001
This is incomplete. You also need to disable ^U or the user can still erase the whole pending line. Also ^W. There may be more; check. Safer approach would be disabling ICANON mode entirely.
R..
R. - you are correct. The OP specs did not include that, IMO. And you would need to read through the entire struct termios turning off possibilities right? As mentioned elsewhere a complete solution is not what I posted.
jim mcnamara
A: 

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.

asc99c