Yan*_*Xie 0 c++ terminal escaping ansi-escape
我想在编程中模拟退格键并按如下方式实现。
// del.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "123456";
cout << "\b\b\b" /* backspace key */<< '\x7f' /* DEL key */ << '\x7f' << '\x7f';
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我得到这样的结果

如何获得如下所示的结果而不需要用空格替换尾部
123
Run Code Online (Sandbox Code Playgroud)
也就是说如何删除而不是替换光标后退格的那些字符。
使用“清除到行尾”转义序列,CSI K。
cout << "123456";
cout << "\b\b\b\033[K";
cout << endl;
Run Code Online (Sandbox Code Playgroud)
有关转义序列的列表,请参阅ANSI 转义代码(维基百科)。当然,并非所有这些都可以在所有终端上运行,但如今,有了软件终端,我就不用担心了。