【问题标题】:Cannot cout string when boost's io service is runningboost的io服务运行时无法计算字符串
【发布时间】:2016-10-21 11:32:14
【问题描述】:

我有一些简单的代码:

#include <iostream>
#include <string>
int main() {
    boost::asio::io_service ioservice;
    TCPServer server(ioservice);
    std::cout << "hello world";
    ioservice.run();
}

我找到了上面的 hello world 字符串。虽然当我调用service.run() 时,我的终端上没有输出字符串。当我删除最后一个调用表达式时,可以看到输出。让我烦恼的是,我在通话之前就开始了 cout 并且没有输出任何内容。即使在我的 TCP 服务器上定义的处理程序上, cout 也似乎不可用。那么人们在使用 Boost Asio 时如何登录到标准输出呢?

【问题讨论】:

  • 如果你使用endl来刷新cout呢?
  • 试试:std::cout &lt;&lt; "hello world" &lt;&lt; std::flush; 它可能只是在等待输出缓冲区填满。

标签: c++ boost boost-asio


【解决方案1】:

您在输入.run() 之前没有刷新流,因此您的信息仍在缓冲区中。

使用std::cout &lt;&lt; std::flush(或std::cout &lt;&lt; std::endl 也包含换行符)。

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多