使用'b'字符的意外控制台行为
Unexpected console behaviour using '' characters
我的程序中有这样的代码:
std :: cout << process_prompt << std :: left << std :: setw( 40 ) << "Looping on tree: clust_tree.";
some_function();
std :: cout << std :: string( 52, 'b' );
some_function()有一个循环:
for( Int_t i = 0; i < n_entries; ++i )
{
(some stuff...)
if( i % 100000 == 0 )
{
percent_done = static_cast<float>(i) / n_entries;
std :: cout << std :: setw( 4 ) << static_cast<int>(percent_done * 100) << "p" << std :: flush;
std :: cout << std :: string( 5, 'b' ) << std :: flush;
}
}
process_prompt只是我用来写一条彩色消息的一种奇特方式,上面写着"process:"。
我期望发生的是(每一行都是前一行的更新):
Process: Looping on tree: clust_tree. 0 p
Process: Looping on tree: clust_tree. 10 p
...
实际情况:
Process: Looping on tree: clust_tree. 0 p
12 pss: Looping on tree: clust_tree. 0 p
54 pss: Looping on tree: clust_tree. 0 p
不知怎么的,我的光标总是移到行的开头。有人能告诉我我做错了什么吗?谢谢你的回复!
发现错误:我还有一些其他的代码部分,在循环中输出单个/b字符。
注意:这不是问题的答案,不确定这是否适用于在线编译器,因此在此处编写完整示例的代码。。
这个完整的例子对我来说很好(c++11,gcc 4.8.2)
#include <iostream>
#include <chrono>
#include <thread>
#include <iomanip>
using namespace std;
int main(void) {
cout << "Completed " << flush;
for (auto i = 1; i <= 100; ++i) {
cout << setw(4) << i << "%" << flush << string(5, 'b') << flush;
this_thread::sleep_for(chrono::milliseconds(100));
}
cout << endl << "Done" << endl;
}
好吧,我似乎找到了答案,实际上每个循环都有一个额外的'/b'。谢谢大家!
相关文章:
- C++:将控制台输出存储在宏中更好吗
- 在C++中对T*类型执行std::move的意外行为
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++控制台应用程序阻止退出
- 如何修复此iFile以将.txt输出到控制台
- 为什么控制台要求输入,即使代码中没有输入
- 处理除以零会导致<csignal>意外行为
- vscode下的Arduino代码出现意外编译错误
- 使用++运算符会导致意外的结果
- 套接字读取后,我在缓冲区中看到意外输入
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 使用vscode调试时,GDB意外退出
- 此测试()中发生了什么意外过程?为什么总是覆盖 ch[0 1 2..]?
- 尝试将字符串/字符转换为整数会产生意外结果
- 控制台输出在 Qt5 中未正确显示
- C++出现控制台错误.我无法识别源代码的问题
- 使用'b'字符的意外控制台行为