how to check for the "backspace" character in C
The problem with reading Backspace is that most terminals are 'cooked' in that keys like backspace are handled by the terminal driver. However, the curses function getch()
can read the backspace as it's not tied to the terminal.
Edit
I just noticed your code is using getch()
for input. I ran a little test program and getch()
returns 127 when you hit backspace. Therefore try:
if (key == 127 || key == 8) { ... /* Checks for both Delete or Backspace */
Also note that your sample code uses the assignment operator =
when it should be using the equality operator ==
The type of i/o stream may helps. Standard input stream is a kind of line buffered stream, which do not flush until you write a '\n' char into it. Full buffered stream never flush until the buffer is full. If you write a backspace in full buff stream, the '\b' may be captured.
Reference the unix environment advantage program.