c++回车换行长字符串后跟较短的字符串

标签 c++ windows console-application carriage-return

我有一个要打印状态消息的应用程序。但是,有时会出现较短的状态消息跟在较长的状态消息之后,这会导致以下情况:

长消息:

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 上工作

注意:我已经尝试用 \bspaces 覆盖该行,但这可能会导致在多行清理中删除了我的 \r 方法的功能。

最佳答案

我会在您的案例的前一个输出的长度中重复使用 '\b'(退格键)。好像挺好看的standardized :

cout << StatusMessage << '\r';
cout << std::string(StatusMessage.size(),'\b');

关于c++回车换行长字符串后跟较短的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45639622/

相关文章:

C++ 单例实现 : pimpl idiom for singletons, 优点和缺点

c++ - 我怎样才能产生一个只在给定时间内存在的进程

c++ - SWIG:重用选择性异常处理程序?

python - 从 re.sub 调用函数

C# 是否有一种紧凑的方法来获取二维数组中的多个坐标?

c++ - 是否有 boost::weak_intrusive_pointer?

windows - 适用于 Windows 的 Lazarus 64 位可以编译为适用于 Windows 的 64 位吗?

c++ - 我收到错误 "LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup"

来自 appsettings 的 C# 强类型 ConfigurationBuilder List<object>

c++ - 文件 i/o 无限循环 C++ eof