繁体   English   中英

使用'\\ b'字符的意外控制台行为

[英]Unexpected console behaviour using '\b' 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”。 感谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM