【问题标题】:Why does Qt Creator add a newline after cout.flush() call?为什么 Qt Creator 在 cout.flush() 调用后添加换行符?
【发布时间】:2014-11-29 13:04:05
【问题描述】:

代码很简单

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    for(int i = 0; i < 3; ++i)
    {
        cout << "1 "; cout.flush();
        sleep(1);
    }
}

在 .pro 文件中

QT += core    
TARGET = ProjectName    
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets    
TEMPLATE = app

如果输出进入Qt Creator“应用程序输出”(默认源代码下的地方),每个“1”从一个新行开始。

如果我使用 g++ 在 Ubuntu 终端中编译和执行单独的 *.cpp 文件,它会以正确的方式工作。

如果我在 Qt Creator 中创建一个新项目并输出到 Qt Creator 控制台(带有黑色背景的新窗口),它会以正确的方式工作。

那么,为什么 cout.flush() 在第一种情况下会导致换行?

【问题讨论】:

    标签: c++ qt-creator cout flush


    【解决方案1】:

    显然,这是一个 Qt Creator 错误,他们不打算很快解决(至少 2015 年 4 月的状态是这样)。我已经测试了 Qt Creator 3.5.1 (Qt 5.5.1) 并且错误仍然存​​在,即使新行仅在第一个 std::cout.flush() 调用或将 std::flush 操纵器附加到 std::cout &lt;&lt; 调用之后打印。

    无论如何,这个错误的 JIRA 票可以在这里找到:

    Flushing application output automatically starts a new line

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      相关资源
      最近更新 更多