backspace escape character '\b' is used t bring the cursor one character back...so it must work like a backspace key on our keyboard.
cout<<"Learn c++\b!";
This gives normal expected result- Learn c+!
But when using \b as the last character
cout<<"Learn c++!\b";
The '!' is not erased output-Learn c++!
Instead i have to use
cout<<"Learn c++!\b \b";
to get the output-Learn c++
Can anybody tell the reason for such behavior..?
Your assumption:
so it must work like a backspace key
is not correct. \b
moves the cursor one character backwards. It does not delete any characters. In an interactive terminal that leads to the effect you observed.
std::cout << "Hello, world\b!\n";
This starts with printing Hello, world
, then moves the cursor 1 step to the left, then prints !
and moves to a new line, resulting in Hello, worl!
.
std::cout << "Hello, world!\b\n";
This starts with printing Hello, world!
, then moves the cursor 1 step to the left, then moves to a new line resulting in Hello, world!
.
As the question comments already mentioned this overwriting effect only happens in a terminal. If you output to a file all the characters including the \b
are preserved as is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.