Good day, how do you know if there is a backspace (\b) in an input string? cuz from what i know, i just move the cursor back, rubs out the nearest character where \b is written. How will i know that there is a \b in there? Tnx
Asked
Active
Viewed 143 times
1 Answers
2
You can look for any character in a string with strchr
. That said, you will not normally see backspaces in your input because your terminal (tty) will be in cooked mode (instead of raw mode) which means that the kernel will be handling things like backspace within a line until you hit return and then the entire (edited) string will be passed to your program.
Programs like your shell put the tty in raw mode so they can do more extensive editing (usually full Emacs or vi-like editing). When you launch a program (such as your own) from a shell it puts it back into cooked mode first.

Ben Jackson
- 90,079
- 9
- 98
- 150
-
ohhh so thats how it works, how about getchar().. checking every input like so while(c = getchar() != EOF) and so on, i have an if statment like this if(c == '\b') but the problem is it wont detect.. so that being said, it is also cooked? if so, is there any way to detect this? – lemoncodes Dec 17 '12 at 01:11
-
You can use curses and this answer: http://stackoverflow.com/questions/7772341/how-to-get-a-character-from-stdin-without-waiting-for-user-to-put-it Or here's a more low level answer; http://stackoverflow.com/questions/2548434/get-an-input-from-keyboard-without-return-in-c – Ben Jackson Dec 17 '12 at 01:14
-
One of those answers mentions *cbreak* mode, which is halfway between cooked and raw. See: http://en.wikipedia.org/wiki/POSIX_terminal_interface – Ben Jackson Dec 17 '12 at 01:15
-
1I would avoid the use of the ancient and often confusing terms "cooked" and "raw" (and "line mode" and "cbreak") and instead use the same term POSIX now uses uniformly: "canonical mode". The terminal interface is either in _canonical mode_ or it's not, and if it's not then certain parameters control when characters are made available, and how many are made available at a time. – Greg A. Woods Dec 17 '12 at 02:08