我有一个要打印状态消息的应用程序。但是,有时会出现较短的状态消息跟在较长的状态消息之后,这会导致以下情况:
长消息:
this is the long status message which is longer than the short one
较短的消息:
This is the short status messagewhich is longer than the short one
//this one should end here ^
我使用的代码是:
cout << StatusMessage << '\r';
我怎样才能克服这个问题并在打印新行之前先删除整行?最好使用跨平台解决方案,但目前我在 Windows 上工作
注意:我已经尝试用 \b
或 spaces
覆盖该行,但这可能会导致在多行清理中删除了我的 \r
方法的功能。
最佳答案
我会在您的案例的前一个输出的长度中重复使用 '\b'
(退格键)。好像挺好看的standardized :
cout << StatusMessage << '\r';
cout << std::string(StatusMessage.size(),'\b');
关于c++回车换行长字符串后跟较短的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639622/