[英]The use of backspace character at the end of the string
backspace escape character '\b' is used t bring the cursor one character back...so it must work like a backspace key on our keyboard.退格转义字符 '\b' 用于将 cursor 带回一个字符......所以它必须像我们键盘上的退格键一样工作。
cout<<"Learn c++\b!";
This gives normal expected result- Learn c+!这给出了正常的预期结果 - 学习 c+!
But when using \b as the last character但是当使用 \b 作为最后一个字符时
cout<<"Learn c++!\b";
The '!'这 '!' is not erased output-Learn c++!
不擦除输出——学c++!
Instead i have to use相反,我必须使用
cout<<"Learn c++!\b \b";
to get the output-Learn c++得到输出-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. \b
将 cursor 向后移动一个字符。 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 !
首先打印
Hello, world
,然后将 cursor 向左移动 1 步,然后打印!
and moves to a new line, resulting in Hello, worl!
并移动到新的一行,结果是
Hello, worl!
. .
std::cout << "Hello, world!\b\n";
This starts with printing Hello, world!
这从打印
Hello, world!
开始。 , then moves the cursor 1 step to the left, then moves to a new line resulting in Hello, world!
,然后将 cursor 向左移动 1 步,然后移动到新的一行,结果是
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.如果您将 output 保存到文件中,则包括
\b
在内的所有字符都将按原样保留。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.