Erasing Using Backspace Control Character

Erasing using backspace control character

The usual way of erasing the last character on the console is to use the sequence "\b \b". This moves the cursor back one space, then writes a space to erase the character, and backspaces again so that new writes start at the old position. Note that \b by itself only moves the cursor.

Of course, you could always avoid outputting the comma in the first place:

if(i > 0) cout << ",";
cout << a[i];

How to erase last char in console using backspace, without using space char?

Presumably, you need to remove the character from inBuffer (by decrementing inBufIdx and overwriting the old character with a 0). What the screen shows should be irrelevant.

By the way, the backspace-space-backspace sequence won't work at the right margin

What is the use of '\b' (backspace)

Backspace is a control character that moves the cursor one character back in the console but doesn't delete it.

What's the reason for the backspace character, and how might it be used?

It was used historically in the ASCII world to print accented characters.

For example à could be produced using the three character sequence a Backspace ` (or, using the characters' hex values, 0x61 0x08 0x60).

See more on backspace here

Backspace key vs Backspace character

A lot of people confuse the two. Backspace key on keyboard has almost the universal function to delete the previous character (= move cursor back and delete that character). The backspace character '\b' however only moves the cursor one position back in the console window and doesn't delete it.

Can backspace escape cancel a new-line escape?

Your question goes beyond the scope of the C language: printf("Hello\n\b world"); outputs the bytes from the format string, possibly translated according to the text mode handling of newlines:

  • on unix systems, the bytes are output to the system handle unmodified.

  • on Microsoft legacy systems, the newline is converted to CR LF and the other bytes transmitted unmodified.

If the standard output is directed to a file, the file will contain the translation of the newline and a backspace (0x08 on most systems).

If the standard output goes to a terminal, the handling of the backspace special character is outside the program's control: the terminal (hardware, virtual, local or remote...) will perform its task as programmed and configured... Most terminals move the cursor left one position on whatever display they control, some erase the character at that position. If the cursor is already at column 1, it is again system dependent whether backspace moves the cursor back to the end of the previous line, whatever that means. Many systems don't do that and keep the cursor at column 1. This seems consistent with the behavior you observe.

The backspace escape character '\b': unexpected behavior?

Your result will vary depending on what kind of terminal or console program you're on, but yes, on most \b is a nondestructive backspace. It moves the cursor backward, but doesn't erase what's there.

So for the hello worl part, the code outputs


hello worl
^

...(where ^ shows where the cursor is) Then it outputs two \b characters which moves the cursor backward two places without erasing (on your terminal):


hello worl
^

Note the cursor is now on the r. Then it outputs d, which overwrites the r and gives us:


hello wodl
^

Finally, it outputs \n, which is a non-destructive newline (again, on most terminals, including apparently yours), so the l is left unchanged and the cursor is moved to the beginning of the next line.

character for delete, like there's /b for backspace?

No, and there is no backspace either, in the sense implied in the question. The notation \b denotes the control character U+0008 BACKSPACE, which may have some effect on some devices but does not usually do anything on modern devices and in modern programs. (And /b is just two Ascii characters.)

You can see by testing with alert('FOO\bBAR'.length). It shows 7, since BACKSPACE is just one character there. It has no visible form, and it does not remove anything or make anything else invisible.

Other control characters are similarly ignored, except for characters related to line breaks and the HORIZONTAL TAB, which may have special effects when included in document content. Control characters were included in the Ascii code for use in various device control operations. Modern programming languages may still have escape notations for such characters, but this does not mean that they should be expected to have practical use nowadays.

How can I make backspace not count as a character?

void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";

i = 0;
do
{
ch = _getch();
if (ch == 8)
{
i--;
cout << "\b \b";
continue;
}

password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout << "*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';

if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout << "\n\nThe program is executed !";
cin.get();
}

Not the cleanest code but it works. Decrement the counter to over write the previous character and output two backspace characters separated by a space.

Backspace character weirdness

What you are seeing is correct. Backspace or ^H moves the cursor to the left, no erasing. To erase a character, you need to output ^H ^H (Backspace-Space-Backspace).


To answer your comment - Backspace is defined that way in the VT100/ANSI family of terminals, from which a lot of terminal control code sequences borrow. See the VT100 user manual here which defines the function of BS as "Moves cursor to the left one character position, unless it is at the left margin, in which case no action occurs". In other words it's a quirk of history :)

As to why it was defined this way initially - I guess it's more flexible to have a non destructive cursor movement control code, as destructive backspace can be implemented as shown above.



Related Topics



Leave a reply



Submit